NSNumberFormatter trims values on encountering zero after decimal - objective-c

It can be a duplicate question,but whatever i found it didn't help me.
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setGroupingSeparator:#","];
[numberFormatter setGroupingSize:3];
[numberFormatter setUsesGroupingSeparator:YES];
[numberFormatter setDecimalSeparator:#"."];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:2];
NSString *theString = [numberFormatter stringFromNumber:num];
I'm using this for thousands string conversion.But if i pass values like 5123.00 it converts it to 5,123,but i need all the decimal values(like 5,123.00) after decimal point, even it is zero.How to do this,please provide some solution?

Add
[numberFormatter setMinimumFractionDigits:2];

Related

How to convert numbers into indicative string

I just want to know how can I can convert the numbers 1, 2 or 3 into First, second or Third ?
Any help would be appreciated.
Try this,
- (NSString *)getOrdinalTextFor:(NSInteger)number {
NSNumber *numberValue = [NSNumber numberWithInteger:number];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterOrdinalStyle];
return [numberFormatter stringFromNumber:numberValue];
}
See this one may be it helps you,
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
NSString* numberString = [formatter stringFromNumber:[NSNumber numberWithInt: 3]];
NSLog(#"%#",numberString);
enter link description hereDownload Formatter kit from GitHub
TTTOrdinalNumberFormatter *ordinalNumberFormatter = [[TTTOrdinalNumberFormatter alloc] init];
[ordinalNumberFormatter setGrammaticalGender:TTTOrdinalNumberFormatterMaleGender];
NSNumber *number = [NSNumber numberWithInteger:2];
NSString *str = [ordinalNumberFormatter stringFromNumber:number];
NSLog(#"%#", str);
Read up this link, its very helpful: Number Formatting

How to avoid grouping separator in NSNumberFormater until 5 digits

I have an application that needs to display metric units in millimeters and based on the research that I have done (e.g. http://physics.nist.gov/cuu/pdf/sp811.pdf), it is recommended that you don't add grouping separators until you have 5 digits. Is this possible with NSNumberFormatter? I know I could test the value and enable/disable grouping based on its size but I would rather use the appropriate configuration for the NSNumberFormatter if it exists. (BTW, I am on Mac OS X) So for example:
3456 should be shown as 3456 mm
10234 should be shown as 10,234 mm (assuming comma is the grouping separator)
I have read the Apple docs and didn't see anything. I experimented with different searches to find the answer on Stackoverflow but couldn't find a similar question.
Here is how I am currently setting up my NSNumberFormatter:
if (numberFormatter == nil) {
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale currentLocale]];
[numberFormatter setMinimumFractionDigits:0];
[numberFormatter setMaximumFractionDigits:2];
}
NSNumber *numberInMM = [NSNumber numberWithFloat:fLenInMMs];
NSString *numberStr = [numberFormatter stringFromNumber:numberInMM];
numberStr = [numberStr stringByAppendingString:#" mm"];
long long d = fLenInMMs;
if (d>9999) {
NSNumberFormatter *numberFormatter=nil;
if (numberFormatter == nil) {
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale currentLocale]];
[numberFormatter setMinimumFractionDigits:0];
[numberFormatter setMaximumFractionDigits:2];
}
NSNumber *numberInMM = [NSNumber numberWithFloat:fLenInMMs];
NSString *numberStr = [numberFormatter stringFromNumber:numberInMM];
numberStr = [numberStr stringByAppendingString:#" mm"];
NSLog(#"%#", numberStr);
}
else {
NSNumber *numberInMM = [NSNumber numberWithFloat:fLenInMMs];
NSString *numberStr = [NSString stringWithFormat:#"%# mm",numberInMM];
}
EDIT
You can also subclass NSNumberFormatter and implement
- (NSString *)stringFromNumber:(NSNumber *)number
{
NSNumber *nm = [NSNumber numberWithInt:9999];
if (NSOrderedAscending==[number compare:nm]) {
return [NSString stringWithFormat:#"%# mm",number];
}
return [super stringFromNumber:number];
}

NSNumberFormatter with NSNumberFormatterCurrencyStyle: numberFromString for negative values appears to be off by one

I am running into an apparent off-by-one issue with numberFromString on a NSNumberFormatter with NSNumberFormatterCurrencyStyle and a negative minimum value. As you can see from the test below, numberFromString returns nil when trying to convert the minimum amount, but returns correctly with one under the minimum.
Am I misunderstanding what "minimum' means in the negative context? Or is this a bug in numberFromString? or?
Thanks.
- (void)test {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setMinimum:[NSDecimalNumber decimalNumberWithMantissa:2142 exponent:-2 isNegative:YES]];
[numberFormatter setMaximum:[NSDecimalNumber decimalNumberWithMantissa:100 exponent:0 isNegative:NO]];
[numberFormatter setNegativeFormat:[#"-" stringByAppendingString:[numberFormatter positiveFormat]]];
NSLog(#"[numberFormatter positiveFormat] is: %#", [numberFormatter positiveFormat]);
NSLog(#"[numberFormatter negativeFormat] is: %#", [numberFormatter negativeFormat]);
NSLog(#"[numberFormatter minimum] is: %#", [numberFormatter minimum]);
NSLog(#"[numberFormatter maximum] is: %#", [numberFormatter maximum]);
NSLog(#"[numberFormatter numberFromString:#\"-$21.43\"] is: %#", [numberFormatter numberFromString:#"-$21.43"]);
NSLog(#"[numberFormatter numberFromString:#\"-$21.42\"] is: %#", [numberFormatter numberFromString:#"-$21.42"]);
NSLog(#"[numberFormatter numberFromString:#\"-$21.41\"] is: %#", [numberFormatter numberFromString:#"-$21.41"]);
NSLog(#"[numberFormatter numberFromString:#\"-$21.40\"] is: %#", [numberFormatter numberFromString:#"-$21.40"]);
}
Console output:
2012-07-16 10:13:04.242 Am[21075:15203] [numberFormatter positiveFormat] is: ¤#,##0.00
2012-07-16 10:13:04.244 Am[21075:15203] [numberFormatter negativeFormat] is: -¤#,##0.00
2012-07-16 10:13:04.244 Am[21075:15203] [numberFormatter minimum] is: -21.42
2012-07-16 10:13:04.245 Am[21075:15203] [numberFormatter maximum] is: 100
2012-07-16 10:13:04.246 Am[21135:15203] [numberFormatter numberFromString:#"-$21.43"] is: (null)
2012-07-16 10:13:04.247 Am[21075:15203] [numberFormatter numberFromString:#"-$21.42"] is: (null)
2012-07-16 10:13:04.248 Am[21075:15203] [numberFormatter numberFromString:#"-$21.41"] is: -21.41
2012-07-16 10:13:04.248 Am[21135:15203] [numberFormatter numberFromString:#"-$21.40"] is: -21.4

Formatting a NSNumber as percentage (%) with one decimal place using NSNumberFormatter

I am trying to format a NSNumber containing a value of 0.305 as "30.5%".
However, my following code does not work:
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setLocale:[NSLocale currentLocale]];
How can I specify that the number should be formatted as percentage with one decimal place?
This works for me:
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMinimumFractionDigits:1];
Edited for the actual correct answer:
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMinimumFractionDigits:1];
Try
float percent = 0.305;
NSString *formattedPercentText = [NSString stringWithFormat:#"%.1f%%", percent * 100];
I'm using also setMaximum
float precent = 3.5;
NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMinimumFractionDigits:1];
[numberFormatter setMaximumFractionDigits:1];
return [numberFormatter stringFromNumber:[NSNumber numberWithFloat:precent]];

How to truncate .000 zeros from double with NSNumberFormatter

Ok so if I have a double to begin with how do I limit it or truncate digits to 1dp since I know they're not needed to be viewed
double a = 1.6;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:a]];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGeneratesDecimalNumbers:YES];
[numberFormatter setMaximumFractionDigits:1];
[numberFormatter setMinimumFractionDigits:1];
[numberFormatter setAlwaysShowsDecimalSeparator:YES];
NSLog(#"a raw: %f", a); //outputs 1.600000
myLabel.text = [NSString stringWithFormat:#"%#",numberAsString];
NSLog(#"Formatted a: %#", numberAsString); //outputs 2
[numberFormatter release];
So the problem is it rounds the value and leaves no decimal place, I'm trying to get it to output just 1.6 for myLabel.
I am not sure but you can try with NSString *formatted = [NSString stringWithFormat:#"%.1f", a];, then use the formatted as the label text