Limit a double to two decimal places

Posted by Jacob on Stack Overflow See other posts from Stack Overflow or by Jacob
Published on 2009-07-11T10:19:00Z Indexed on 2010/06/01 15:23 UTC
Read the original article Hit count: 188

Filed under:
|

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

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

ie 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 twos:

[NSString stringWithFormat: @"%.2f", total]

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about formatting