This question already has answers here:
Easiest way to format a number with thousand separators to an NSString according to the Locale
(5 answers)
Closed 8 years ago.
I'm looking for a way to format a string into number and show it on textfield
For example: I have the number "1000", I want to it convert into "1.000" or 100000 into 100.000. Or 1000000 into 1.000.000. Please help me if you know solution for this
Thanks
If you want to format a number as currency, then you need to use NSNumberFormatter. However, the other answers are wrong because they tell you to use the wrong numberStyle or tell you to change the format string. These are both incorrect.
First, you want an NSNumberFormatter:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
Next, you need to tell it you're going to be formatting things for currency:
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
Optionally, if you want things to be formatted in a specific currency, you may set the currency code:
formatter.currencyCode = #"USD";
Optionally, if you want things to be formatted according to the locale settings that are not the user's default locale, you may set the locale:
formatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_GB"];
If you were to run those four lines of code, you would get an NSNumberFormatter that formats an NSNumber in US Dollars, but according to the Great Britain locale. The locale is what defines the placement of the currency symbol (before or after the number), the decimal separator (. vs ,), the thousands separator (, vs ), and so on.
The currency code defines what the currency symbol is ($ vs £, etc).
By default, the locale is [NSLocale currentLocale], and the currency code is the code for the current locale.
One very very very important thing to remember, however, is that this only formats currency for you. It does not perform any currency conversion for you. It assumes that whatever NSNumber you give it is already in units of the target currency.
You can use the NSNumberFormatter class as follows.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLog(#"%#", [formatter stringForObjectValue:#2333999878]);
This class is very powerful and will also allow you to localize the currency beside the formatting
You can do like this:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat:#"#.###"];
NSString *string = [numberFormatter stringFromNumber:[NSNumber numberWithInt:1000]];
Related
I know the way to set formatted float with specific number
NSLog(#"%.2f", myFloat);
What is the way to set parameter number? Something like this
cell.lblOpenPrice.text = [NSString stringWithFormat:#"%.%if", trade.open_price, trade.digits];
You should use an instance of NSNumberFormatter for this. There are dozens of options, too many to discuss them here. I. e. you can set the total number of digits (significant digits) or the number of integer and fraction digits as discussed here.
NSNumberFormatter *formatter = [NSNumberFormatter new];
// Do the desired configuration
NSString *text = [formatter stringFromNumber:#(myFloat)];
To set a field precision dynamically you use an asterisk in the format and provide the precision argument first, so your code example is:
cell.lblOpenPrice.text = [NSString stringWithFormat:#"%.*f", trade.digits, trade.open_price];
HTH
I download some Json data that has inside it a price, the price gets downloaded as double so for example if I have 10.50 the value assigned to my variable will be 10.5, how can I keep the 0 after the first decimal number?
This is the code I used to create the NSNumber:
NSNumber *numPrice = jsonElement[#"Price"]; //the json is 10.50 but numPrice becomes 10.5
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundUp];
NSString *numberString = [formatter stringFromNumber:numPrice];
For output purposes you can set your NSNumberFormatterto have exactly 2 decimal digits like
[formatter setMinimumFractionDigits:2];
[formatter setMaximumFractionDigits:2];
This is for displaying two decimal numbers. Internally your NSNumber will be stored of course with a single digit, if possible.
Any trailing zeros are simple a display issue, they are no part of the number.
You can't. When a JSON document contains 10.50, it is the number 10.5. There is no difference between 10.5000000 or 10.50 or 10.5 in a JSON document. They are absolutely one hundred percent the same thing.
You can feel free to display this number any way you like.
You can't, the thing that you have to do if you want to show the numbers with two decimals after saving them as a NSNumber is print them as a float with two decimals. Something like that:
NSNumber *numPrice = jsonElement[#"Price"]; //the json is 10.50 but
NSString *numberString = [NSString stringWithFormat:#"%0.2f", numPrice.floatValue];
This question already has answers here:
Convert string to float in Objective-C
(2 answers)
Closed 8 years ago.
I am building a calculator, and I have the following:
firstOperand = #"2883"
secondOperand = #"10"
operator = #"/" // division
How would I get the result here as an NSString? Here is how I would do the equivalent in Python:
result = str (float(firstOperand) / float(secondOperand))
How would I do the same in Obj-C?
Yo have two ways to do so.
If your string only contains the number and you are certain it is "well-written" you can just use
int i = [yourString intValue];
or floatValue, or doubleValue or integerValue ...
If your string is not well formatted, and you need something more powerful to extract the data, you need to use [NSNumberFormatter][1]. For instance:
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber* number = [formatter numberFromString:yourString];
Code is ARC.
Instances of NSNumberFormatter format the textual representation of
cells that contain NSNumber objects and convert textual
representations of numeric values into NSNumber objects. The
representation encompasses integers, floats, and doubles; floats and
doubles can be formatted to a specified decimal position.
NSNumberFormatter objects can also impose ranges on the numeric values
cells can accept.
I have some string s that is locale specific (eg, 0.01 or 0,01). I want to convert this string to a NSDecimalNumber. From the examples I've seen thus far on the interwebs, this is accomplished by using an NSNumberFormatter a la:
NSString *s = #"0.07";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setGeneratesDecimalNumbers:YES];
NSDecimalNumber *decimalNumber = [formatter numberFromString:s];
NSLog([decimalNumber stringValue]); // prints 0.07000000000000001
I'm using 10.4 mode (in addition to being recommended per the documentation, it is also the only mode available on the iPhone) but indicating to the formatter that I want to generate decimal numbers. Note that I've simplified my example (I'm actually dealing with currency strings). However, I'm obviously doing something wrong for it to return a value that illustrates the imprecision of floating point numbers.
What is the correct method to convert a locale specific number string to an NSDecimalNumber?
Edit: Note that my example is for simplicity. The question I'm asking also should relate to when you need to take a locale specific currency string and convert it to an NSDecimalNumber. Additionally, this can be expanded to a locale specific percentage string and convert it to a NSDecimalNumber.
Years later:
+(NSDecimalNumber *)decimalNumberWithString:(NSString *)numericString in NSDecimalNumber.
Based on Boaz Stuller's answer, I logged a bug to Apple for this issue. Until that is resolved, here are the workarounds I've decided upon as being the best approach to take. These workarounds simply rely upon rounding the decimal number to the appropriate precision, which is a simple approach that can supplement your existing code (rather than switching from formatters to scanners).
General Numbers
Essentially, I'm just rounding the number based on rules that make sense for my situation. So, YMMV depending on the precision you support.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setGeneratesDecimalNumbers:TRUE];
NSString *s = #"0.07";
// Create your desired rounding behavior that is appropriate for your situation
NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:FALSE raiseOnOverflow:TRUE raiseOnUnderflow:TRUE raiseOnDivideByZero:TRUE];
NSDecimalNumber *decimalNumber = [formatter numberFromString:s];
NSDecimalNumber *roundedDecimalNumber = [decimalNumber decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
NSLog([decimalNumber stringValue]); // prints 0.07000000000000001
NSLog([roundedDecimalNumber stringValue]); // prints 0.07
Currencies
Handling currencies (which is the actual problem I'm trying to solve) is just a slight variation on handling general numbers. The key is that the scale of the rounding behavior is determined by the maximum fractional digits used by the locale's currency.
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyFormatter setGeneratesDecimalNumbers:TRUE];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
// Here is the key: use the maximum fractional digits of the currency as the scale
int currencyScale = [currencyFormatter maximumFractionDigits];
NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:currencyScale raiseOnExactness:FALSE raiseOnOverflow:TRUE raiseOnUnderflow:TRUE raiseOnDivideByZero:TRUE];
// image s is some locale specific currency string (eg, $0.07 or €0.07)
NSDecimalNumber *decimalNumber = (NSDecimalNumber*)[currencyFormatter numberFromString:s];
NSDecimalNumber *roundedDecimalNumber = [decimalNumber decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
NSLog([decimalNumber stringValue]); // prints 0.07000000000000001
NSLog([roundedDecimalNumber stringValue]); // prints 0.07
This seems to work:
NSString *s = #"0.07";
NSScanner* scanner = [NSScanner localizedScannerWithString:s];
NSDecimal decimal;
[scanner scanDecimal:&decimal];
NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithDecimal:decimal];
NSLog([decimalNumber stringValue]); // prints 0.07
Also, file a bug on this. That's definitely not the correct behavior you're seeing there.
Edit: Until Apple fixes this (and then every potential user updates to the fixed OSX version), you're probably going to have to roll your own parser using NSScanner or accept 'only' double accuracy for entered numbers. Unless you're planning to have the Pentagon budget in this app, I'd suggest the latter. Realistically, doubles are accurate to 14 decimal places, so at anything less than a trillion dollars, they'll be less than a penny off. I had to write my own date parsing routines based on NSDateFormatter for a project and I spent literally a month handling all the funny edge cases, (like how only Sweden has the day of week included in its long date).
See also Best way to store currency values in C++
The best way to handle currency is to use an integer value for the smallest unit of the currency, i.e. cents for dollars/euros, etc. You'll avoid any floating point related precision errors in your code.
With that in mind, the best way to parse strings containing a currency value is to do it manually (with a configurable decimal point character). Split the string at the decimal point, and parse both the first and second part as integer values. Then use construct your combined value from those.
How do I create a number formatter for formatting large numbers with commas?
For example, 2389378.289 should become 2,389,378.289
Note that there is not always three decimal places, nor is there always a decimal at all. Please use NSNumberFormatter.
Thanks!
NSNumberFormatter *fmt = [[[NSNumberFormatter alloc] init] autorelease];
[fmt setAlwaysShowsDecimalSeparator:NO];
[fmt setFormat:#"#,##0.###"];
NSLog(#"%#",[fmt stringFromNumber:[NSNumber numberWithFloat:35.424252]]);
NSLog(#"%#",[fmt stringFromNumber:[NSNumber numberWithFloat:21.3]]);
NSLog(#"%#",[fmt stringFromNumber:[NSNumber numberWithFloat:10392425]]);
The format Chuck gave you supports up to 3 decimal places. If you have a number with less than 3 they just don't show up. Chuck is also correct that you want to take a look at the Unicode number format guide, it is invaluable for number formatting issues.
The format for that would be #"#,##0.###". Take a look at the Unicode number format guide for a full explanation of how to construct format strings.
(Also note that this works correctly in locales where commas and dots are reversed, or where other characters are used.)