19

How do I achieve the following conversion from double to a string:

1.4324 => "1.43"
9.4000 => "9.4"
43.000 => "43"

i.e. I want to round to to decimal places but dont want any trailing zeros, ie i dont want

9.4 => "9.40" (wrong)
43.000 => "43.00" (wrong)

So this code which I have now doesn't work as it displays excess zeros:

[NSString stringWithFormat: @"%.2f", total]
Jay
  • 18,817
  • 36
  • 115
  • 173
  • 1
    This is not a duplicate of http://stackoverflow.com/questions/4985791/round-double-value-to-2-decimal-places so I think it should be unmarked as a duplicate. This question is about how to limit to a *maximum* of two decimal places whereas the other question is how to limit to *always* two decimals. The latter is easy and already mentioned in original question. – Peter Theill May 27 '15 at 10:58
  • Yea it's not a duplicate. Someone just excited about their mod powers. – Jay May 27 '15 at 22:07

5 Answers5

35

You made simple mistake. This will work:

[NSString stringWithFormat: @"%.2lf", total]
moinudin
  • 126,681
  • 45
  • 187
  • 213
Siva Krishna
  • 483
  • 1
  • 5
  • 7
24

Use NSNumberFormatter. See the Data Formatting Programming Guide's chapter on Number Formatters.

Peter Hosey
  • 94,814
  • 14
  • 206
  • 368
4

The easiest way is to probably roll your own. I've had to do this in C before since there's no way to get the behavior you want with printf formatting.

It doesn't appear to be much easier in Objective-C either. I'd give this a try:

NSString *ftotal = [NSString stringWithFormat: @"%.2f", total];
while ([ftotal hasSuffix:@"0"]) {
    ftotal = [ftotal subStringToIndex [ftotal length]-1];
}
if ([ftotal hasSuffix:@"."]) {
    ftotal = [ftotal subStringToIndex [ftotal length]-1];
}

or this (possibly faster) variant:

NSString *ftotal = [NSString stringWithFormat: @"%.2f", total];
if ([ftotal hasSuffix:@".00"]) {
    ftotal = [ftotal subStringToIndex [ftotal length]-3];
} else {
    if ([ftotal hasSuffix:@"0"]) {
        ftotal = [ftotal subStringToIndex [ftotal length]-1];
    }
}

The stringWithFormat guarantees there will always be a ".nn" at the end (where n is a digit character). The while and if simply strip off trailing zeros and the trailing decimal if it was an integer.

Obviously, you may want to put it in a function or class so you can get at it from anywhere without having to duplicate the code all over the place.

paxdiablo
  • 814,905
  • 225
  • 1,535
  • 1,899
1

I'm not familiar with objective C, but this isn't possible with standard printf-style formatting.

Using %g would sort-of work, but for large or small numbers it would use scientific notation (eg 9.6e+6, 4.2e-7) rather than decimal notation.

The equivalent question was asked for C/C++ here, and in that case the answer is to use %f and then strip any trailing 0's from the string. Not exactly elegant.

Community
  • 1
  • 1
John Carter
  • 52,342
  • 26
  • 107
  • 142
  • Hey, that was my answer you're calling inelegant :-) Actually it's a lot more elegant in Objective-C since strings are actually strings rather than C char arrays with all their foibles. – paxdiablo Jul 11 '09 at 11:27
-2

I am pretty sure that [[NSNumber numberWithFloat:f] stringValue] does exactly what you want.