1

I am trying to format some floats as follows:

1.1500 would be displayed as “$ 1.15”
1.1000 would be displayed as “$ 1.10”
1.0000 would be displayed as “$ 1.00”
1.4710 would be displayed as “$ 1.471”
1.4711 would be displayed as “$ 1.4711”

I tried with

NSString *answer = [NSString stringWithFormat:@"$ %.2f",myvalue];
jscs
  • 63,694
  • 13
  • 151
  • 195
Heena
  • 2,348
  • 3
  • 32
  • 58

4 Answers4

7

This is exactly what NSNumberFormatters are for:

float onefifteen = 1.1500;    // displayed as “$ 1.15”
float oneten = 1.1000;     // displayed as “$ 1.10”
float one = 1.0000;    // displayed as “$ 1.00”
float onefortyseventen = 1.4710;    // displayed as “$ 1.471”
float onefortyseveneleven = 1.4711;    // displayed as “$ 1.4711”

NSNumberFormatter * formatter =  [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setUsesSignificantDigits:YES];   
// The whole number counts as the first "significant digit"
[formatter setMinimumSignificantDigits:3]; 

NSLog(@"%@", [formatter stringFromNumber:[NSNumber numberWithFloat:onefifteen]]);
NSLog(@"%@", [formatter stringFromNumber:[NSNumber numberWithFloat:oneten]]);
NSLog(@"%@", [formatter stringFromNumber:[NSNumber numberWithFloat:one]]);
NSLog(@"%@", [formatter stringFromNumber:[NSNumber numberWithFloat:onefortyseventen]]);
NSLog(@"%@", [formatter stringFromNumber:[NSNumber numberWithFloat:onefortyseveneleven]]);

2011-12-15 19:36:52.185 SignificantCents[49282:903] $1.15
2011-12-15 19:36:52.190 SignificantCents[49282:903] $1.10
2011-12-15 19:36:52.190 SignificantCents[49282:903] $1.00
2011-12-15 19:36:52.191 SignificantCents[49282:903] $1.471
2011-12-15 19:36:52.192 SignificantCents[49282:903] $1.4711

jscs
  • 63,694
  • 13
  • 151
  • 195
1

try this :

   NSString *answer = [NSString stringWithFormat:@"%.02f", myvalue];
Maulik
  • 19,348
  • 14
  • 82
  • 137
  • 1
    it is working fine for first three comments.but not fulfilling last two i.e.for `1.4710 -> 1.471` and `1.4711 -> 1.4711` – Heena Dec 15 '11 at 10:43
  • refer this link http://www.iphonedevsdk.com/forum/iphone-sdk-development/37665-float-string-removing-trailing-zeros.html – Maulik Dec 15 '11 at 10:48
  • In doc it is using ` NSString *answer = [NSString stringWithFormat:@"%g", myvalue];` that is ok for last two.but now for 2nd,3rd case it is showing `1.1000 -> 1.1` and `1.000 -> 1` which is also not acceptable result – Heena Dec 15 '11 at 10:57
0

I have figure out this. you can do this as per below implementation.

Please take a look at below demo code...

NSArray * numbers = [[NSArray alloc] initWithObjects:@"1.1500",@"1.1000",@"1.0000",@"1.4710",@"1.4711",nil];

for (int i=0; i<[numbers count]; i++) {
    NSArray * floatingPoint = [[numbers objectAtIndex:i] componentsSeparatedByString:@"."];
    NSString * lastObject = [[floatingPoint lastObject] stringByReplacingOccurrencesOfString:@"0" withString:@""];
    if ([lastObject length] < 2) {
        NSLog(@"%@",[NSString stringWithFormat:@"$ %.2f",[[numbers objectAtIndex:i] floatValue]]);
    } else {
        NSLog(@"%@",[NSString stringWithFormat:@"$ %g",[[numbers objectAtIndex:i] floatValue]]);
    }
}

Thanks,

MinuMaster

MinuMaster
  • 1,457
  • 1
  • 13
  • 29
  • 1
    the other answer is cleaner and uses better practices, since it uses an object that was made for that instead of messing with strings... – igorsantos07 Oct 11 '12 at 22:48
-2

Read the docs on format specifiers.

NSResponder
  • 16,861
  • 7
  • 32
  • 46