scolfax
scolfax

Reputation: 720

Why doesn't NSNumberFormatter remove the decimal point and trailing zeros?

I want to convert an unsigned long int to an NSString formatted with commas every 3 digits and neither a decimal point nor trailing zeros. I can get the commas into the NSString without any trouble using NSNumberFormatter, but for some reason I can't get the decimal point and zeros to go away.

This is my code:

NSNumberFormatter *fmtr;
unsigned long int intToBeDisplayed;
intToBeDisplayed = 1234567890;
fmtr = [[[NSNumberFormatter alloc] init] autorelease];
[fmtr setUsesGroupingSeparator:YES];
[fmtr setGroupingSeparator:@","];
[fmtr setGroupingSize:3];
[fmtr setAlwaysShowsDecimalSeparator:NO];
NSLog([fmtr stringFromNumber:[NSNumber numberWithUnsignedLong:intToBeDisplayed]]);

The log displays "1,234,567,890.00", appropriately adding the commas but also unfortunately the unwanted ".00". I've tried this construct with int instead of unsigned long int, with smaller integer values, and other constructs from various blogs all to no avail. I'd be very grateful for any suggestions on how to get the decimal point and trailing zeros out of the NSString.

I'm a newbie to Objective-C, becoming on oldbie with respect to the blurry-eyed syndrome trying to solve this problem, but am at that stage where every little lesson learned and success achieved in Objective-C is a source of great joy.

Upvotes: 13

Views: 11170

Answers (4)

JFS
JFS

Reputation: 3152

It might be a little late and the core of your question is answered fine. But I want to bring to mind to avoid forcing the separator and decimal point behavior. It is rather recommended to respect the locale setting of the device to show numbers with the right locale behavior.

1. the NSNumberFormatter:

NSNumberFormatter *valueFormatter = [[NSNumberFormatter alloc]init];
valueFormatter.locale = [NSLocale currentLocale];    // <-- allow the device locale setting
// valueFormatter.GroupingSeparator:@","];           // don't force locale behavior
valueFormatter.numberStyle = NSNumberFormatterDecimalStyle;
valueFormatter.usesGroupingSeparator = YES;
valueFormatter.groupingSize = 3;
valueFormatter.maximumFractionDigits = 0;            // this is important to have your primary issue solved

2. show numbers with right locale:

In your case it might not really be necessary to use a NSNumberFormatter since the device will show the separator anyway. In order to built a NSString from primitive data types (e.g. int) with the right locale behavior just use localizedStringWithFormat and format the number as follows:

NSString *testString = [NSString localizedStringWithFormat:@"%lu", intToBeDisplayed];

with having an NSNumber objects:

NSNumber *longNumber = [NSNumber numberWithUnsignedLong:intToBeDisplayed];
NSString *testString = [NSString localizedStringWithFormat:@"%lu", longNumber.unsignedLongValue];

3. show numbers with NSNumberFormatter (right locale):

NSNumber *longNumber = [NSNumber numberWithUnsignedLong:intToBeDisplayed];
NSString *testString = [NSString localizedStringWithFormat:@"%@", [valueFormatter stringForObjectValue:longNumber]];

Using the NSNumber object longNumber in labels or textFields e.g.:

testLabel.text = [NSString localizedStringWithFormat:@"%@", [valueFormatter stringForObjectValue:longNumber]];

would produce the following output on devices with english locale setting:

1,234,567,890

on devices with (e.g.) german locale setting it would automatically show the number as follows:

1.234.567.890

Hope I could add a little bit more to the (difficult) number formatting issue.

Upvotes: 5

Drew H
Drew H

Reputation: 1292

This will remove the decimal point and trailing zeros.

[fmtr setPositiveFormat:@"#"]

and/or

[fmtr setNegativeFormat:@"#"]

Upvotes: 0

David Peckham
David Peckham

Reputation: 526

You can also use setMaximumFractionDigits to truncate the fraction:

unsigned long int intToBeDisplayed = 1234567890;
NSNumber *longNumber = [NSNumber numberWithUnsignedLong:intToBeDisplayed];

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setUsesGroupingSeparator:YES];
[formatter setGroupingSeparator:@","];
[formatter setGroupingSize:3];
[formatter setMaximumFractionDigits:0];

NSString *formattedNumber = [formatter stringFromNumber:longNumber];
NSLog(@"The formatted number is %@", formattedNumber);

Which results in:

The formatted number is 1,234,567,890

This works in iOS 2.0 and later.

Upvotes: 31

Stephen Poletto
Stephen Poletto

Reputation: 3655

This will fix it!

 [fmtr setGeneratesDecimalNumbers:NO];

Output:

2011-03-31 18:40:45.645 Stephen[41552:903] 1,234,567,890

Upvotes: -1

Related Questions