Objective-C: How to format string as $ Price - objective-c

Is their a built-in way of formatting string as $ price, e.g. 12345.45 converted to $12,345.45?

Assuming you are using Cocoa (or just Foundation), you can use NSNumberFormatter and set its style to currency:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
... = [formatter stringFromNumber:number];
By default it uses the locale of your system, but you can change that and lots of other properties, see the NSNumberFormatter API docs.

Assuming the price is held in a float, you probably want +localizedStringWithFormat:.
NSString *priceString = [NSString localizedStringWithFormat:#"$ %'.2f",price];
Hmmm... Apple says they follow the IEEE standard for printf, so it should accept the ' flag, but it doesn't work on Tiger. NSNumberFormatter it is.

You need to get rid of the ' character
So, just have this:
NSString *priceString = [NSString localizedStringWithFormat:#"$ %.2f", price];

NSString *formatedNumbers = [NSNumberFormatter localizedStringFromNumber:myNumber numberStyle:NSNumberFormatterCurrencyStyle];

Related

How to get decimal values when the device is in french using NSNumber?

I have an issue with the textfield. This case works fine with the english language. However, if the device language is change to french (canada) , the textfield is not showing the values after the decimal.
For english:
I enter 99.99 and click Done. The text field display 99.99
For french:
I enter 99,99 and click Done . The text field display 99,00
Here is my code:
_amountField.text = [_amountField currencyFormatFromValue:[NSNumber numberWithDouble:_amountField.text.doubleValue]];
Here the [NSNumber numberWithDouble:_amountField.text.doubleValue] part is returning 99.99 in case of english and 99,00 in case of french
-(NSString *)currencyFormatFromValue:(NSNumber *)value{
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.positiveFormat = #"###.00";
formatter.roundingMode = NSNumberFormatterRoundFloor;
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];
[formatter setGroupingSeparator:groupingSeparator];
[formatter setUsesGroupingSeparator:YES];
[formatter setCurrencySymbol:#""];
[formatter setMinimumIntegerDigits:1];
NSString *formattedValue = [formatter stringFromNumber:value];
return formattedValue;
}
I am not sure, why the french text which is converted in double is terminating the decimal values. Any ideas?
Do I need to convert the text back to english before I sent the value to formatter?
I think your positiveFormat specification with the period is conflicting with NSNumberFormatterCurrencyStyle. I would try commenting out a bunch of lines and recompiling:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
// formatter.positiveFormat = #"###.00";
// formatter.roundingMode = NSNumberFormatterRoundFloor;
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
// NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];
// [formatter setGroupingSeparator:groupingSeparator];
// [formatter setUsesGroupingSeparator:YES];
// [formatter setCurrencySymbol:#""];
// [formatter setMinimumIntegerDigits:1];
NSString *formattedValue = [formatter stringFromNumber:value];
return formattedValue;
In my experience, the formatting out of the box will include grouping separator, currency symbols, rounding mode, etc. Specifying it again can potentially confuse the formatter.
If you really need to explicitly specify the positive format, you can try building the positive format string using the NSLocaleDecimalSeparator constant so that it will be a period or comma as required:
NSString *decimalSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator];
formatter.positiveFormat = [NSString stringWithFormat:#"###%#00", decimalSeparator];
But I don't recommend it!
I found a way to make it work. I need to remove the , character in case of french that occur in the text field and send to formatter .
_amountField.text = [_amountField currencyFormatFromValue:[NSNumber numberWithDouble:[_amountField.text strippedAmount]doubleValue]];
and
//macro
#define IsFrenchLanguage() ([[[NSLocale preferredLanguages] objectAtIndex:0] rangeOfString:#"fr"].location != NSNotFound)
-(NSString *) strippedAmountString
{
NSString * thousandsSeparator = IsFrenchLanguage() ? #" " : #",";
return [self.amountField.text stringByReplacingOccurrencesOfString:thousandsSeparator withString:#""];
}

NSNumberFormatter encoding

I'm using NSNumberFormatter to convert number into RUB currency representation.
This is my code:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterCurrencyStyle];
[formatter setCurrencyCode:#"RUB"];
NSLog(#"String With Number:%#",[formatter stringFromNumber:sum]);
Everything works fine while I use simulator but at real device I keep getting string with broken encoding.
Example:
What I should get:
RUB99.00
What I really get:
99,00¬†—А—Г–±
How could I fix it? And why it happens?

How to format and print float numbers on iOS?

I would like to create a formatted output of a floating point number with correct localization on Cocoa-Touch. The output should be equivalent to that of printf("%<a>.<b>f", n), where <a> is the total number of digits and <f> is the maximum number of fractional digits.
Setup of NSNumberFormatter with <a>=6 and <f>=2: (Platform is iOS 5.1 SDK, Xcode 4.3.3 and the iPhone Simulator 5.1)
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
[nf setNumberStyle:NSNumberFormatterDecimalStyle];
[nf setPaddingCharacter:#" "];
[nf setUsesGroupingSeparator:NO];
[nf setLocale:[NSLocale autoupdatingCurrentLocale]];
[nf setUsesSignificantDigits:YES];
[nf setMaximumSignificantDigits:6];
[nf setMaximumFractionDigits:2];
[nf setRoundingMode:NSNumberFormatterRoundFloor];
NSLog(#"Test: %#", [nf stringFromNumber:[NSNumber numberWithDouble:2.64324897]]);
Expected output (with German locale): Test: 2,64
Observed output (with German locale): Test: 2,64324
Other observations:
I have tried to use different values for the fraction digits, e.g. [nf setMaximumFractionDigits:4] or [nf setMaximumFractionDigits:0]. The result is unchanged, it appears that the fraction digits are ignored. Changing the locale to US only changes the , to a ., not the number of fraction digits.
Question: How can I translate the printf-format string correctly to an NSNumberFormatter?
Ryan is not totally wrong. Use the localizedStringWithFormat method:
using objective-c
NSNumber *yourNumber = [nf numberFromString:yourString];
//to create the formatted NSNumber object
NSString *yourString = [NSString localizedStringWithFormat:#"%.2F", yourNumber];
//to create the localized output
using SWIFT 3
let yourString: String
yourString = String.localizedStringWithFormat("%.2F", yourDoubleNumber) //no need for NSNumber Object
A little bit late but it still might help. Good luck!

Formatting a number to show commas and/or dollar sign

I want to format my UILabel with commas or better with a dollar sign and commas (with no decimal).
Here is the code I am using:
IBOutlet UILabel *labelrev
float rev = (x + y)
labelrev.text = [[NSString alloc] initWithFormat:#%2.f",rev];
I get xxxxxxxxx as the output I want to get xxx,xxx,xxx or $xxx,xxx,xxx
How do I do that?
You should definitely use NSNumberFormatter for this. The basic steps are:
Allocate, initialize and configure your number formatter.
Use the formatter to return a formatted string from a number. (It takes an NSNumber, so you'll need to convert your double or whatever primitive you have to NSNumber.)
Clean up. (You know, memory management.)
This code sets up the number formatter. I've done everything that you want except the currency bit. You can look that up in the documentation.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];
[formatter setGroupingSeparator:groupingSeparator];
[formatter setGroupingSize:3];
[formatter setAlwaysShowsDecimalSeparator:NO];
[formatter setUsesGroupingSeparator:YES];
Next, you want to set up your number and return a formatted string. In your case, we wrap a double in an NSNumber. I do it inline, but you can break it up into two steps:
NSString *formattedString = [formatter stringFromNumber:[NSNumber numberWithFloat:rev];
Don't forget to clean up!
[formatter release];
A quick note about localization:
The NSLocale class provides some useful info about the user's locale. In the first step, notice how I used NSLocale to get a localized grouping separator:
NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];
(Some countries use a full-stop/period, while others use a comma.) I think there's a way to get a localized currency symbol as well, but I'm not one hundred percent sure, so check the documentation. (It depends upon what your trying to do.)
You will need to use a NSNumberFormatter which supports currency.
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSLog(#"%#", [currencyFormatter stringFromNumber:[NSNumber numberWithInt:10395209]]);
[currencyFormatter release];
Prints: $10,395,209.00
[formatterCurrency setMaximumFractionDigits:0]
is only way to trancate decimal digits and decimal separator in a NSNumberFormatterCurrencyStyle formatter.
NSNumberFormatter *formatterCurrency;
formatterCurrency = [[NSNumberFormatter alloc] init];
formatterCurrency.numberStyle = NSNumberFormatterCurrencyStyle;
[formatterCurrency setMaximumFractionDigits:0];
[formatterCurrency stringFromNumber: #(12345.2324565)];
result
12,345 $

How to add commas to number every 3 digits in Objective C?

If I have a number int aNum = 2000000 how do I format this so that I can display it as the NSString 2,000,000?
Use NSNumberFormatter.
Specifically:
NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; // this line is important!
NSString *formatted = [formatter stringFromNumber:[NSNumber numberWithInteger:2000000]];
[formatter release];
By default NSNumberFormatter uses the current locale so the grouping separators are set to their correct values by default. The key thing is to remember to set a number style.
Don't do your own number formatting. You will almost certainly not get all the edge cases right or correctly handle all possible locales. Use the NSNumberFormatter for formatting numeric data to a localized string representation.
You would use the NSNumberFormatter instance method -setGroupingSeparator: to set the grouping separator to #"," (or better yet [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator]; thanks #ntesler) and -setGroupingSize: to put a grouping separator every 3 digits.
There's a static method on NSNumberFormatter that does just what you need:
int aNum = 2000000;
NSString *display = [NSNumberFormatter localizedStringFromNumber:#(aNum)
numberStyle:NSNumberFormatterDecimalStyle];
This way is a little more succinct than creating a new NSNumberFormatter if you don't need to do any additional configuration of the formatter.
Even easier:
NSNumber *someNumber = #(1234567890);
NSString *modelNumberString = [NSString localizedStringWithFormat:#"%#", someNumber];
NSLog(#"Number with commas: %#", modelNumberString);
coworker just taught me this today. #amazing
Think some as i will get this post looking for sample.
So if you are working with number make attention on next params:
setNumberStyle:NSNumberFormatterCurrencyStyle // if you are working with currency
It could be also
setNumberStyle:NSNumberFormatterDecimalStyle
All code is For ARC.
If you are working with Integer and need to get result such as 200,000
int value = 200000;
NSNumberFormatter * formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString * newString = [formatter stringFromNumber:[NSNumber numberWithInteger:value]];
If you are working with Float and need to get result such as 200,000.00
float value = 200000;
NSNumberFormatter * formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2]; // Set this if you need 2 digits
NSString * newString = [formatter stringFromNumber:[NSNumber numberWithFloat:value]];
EDIT
To have ability to use different digital separators use NSLocale.
Add to code where NSLocale is specified on Locale Identifier:
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"de_DE"]];
or use current local:
[formatter setLocale:[NSLocale currentLocale]];
Swift version
let formatter = NSNumberFormatter()
formatter.numberStyle = .DecimalStyle
formatter.maximumFractionDigits = decimalPlaces
let result = formatter.stringFromNumber(NSNumber(double: 8.0))
By http://ios.eezytutorials.com
An easy solution could be this. My answer is almost same like #Nazir's answer but with a small trick.
double current_balance = 2000000.00;
NSNumberFormatter * formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
//[formatter setNumberStyle:NSNumberFormatterCurrencyStyle]; //if you want for currency with $ sign
[formatter setMinimumFractionDigits:2]; // Set this if you need 2 digits
[formatter setMaximumFractionDigits:2]; // Set this if you need 2 digits
NSString * currency_format = [NSString stringWithFormat:#"%#", [formatter stringFromNumber:[NSNumber numberWithDouble:current_balance]]];
For Swift 4.0
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
let result = formatter.string(from: NSNumber(value: 123456))
For those who need to do it with strings of numbers and not just integers (I.e. Big Numbers) I made the following macro:
#define addCommas(__string) (\
(^NSString *(void){\
NSString *__numberString = __string;\
NSString *__integerPortion = __numberString;\
NSString *__decimalPortion = #"";\
if ([__string containsString:#"."]) {\
__integerPortion = [__numberString componentsSeparatedByString:#"."][0];\
__decimalPortion = st(#".%#", [__numberString componentsSeparatedByString:#"."][1]);\
}\
int __i = (int)__integerPortion.length-3;\
while (__i > 0) {\
__integerPortion = st(#"%#,%#", substringInRange(__integerPortion, 0, __i), substringInRange(__integerPortion, __i, (int)__integerPortion.length));\
__i -= 3;\
}\
__numberString = st(#"%#%#", __integerPortion, __decimalPortion);\
return __numberString;\
})()\
)