Search code examples
iphoneobjective-cnsstringnsnumberformatter

NSString - max 1 decimal of a float


I would like to use a float in a NSString. I used the stringWithFormat and a %f to integrate my float into the NSString. The problem is that I would like to display only one decimal (%.1f) but when there is no decimals I don't want to display a '.0' .

How can I do that?

Thanks


Solution

  • I found the answer with NSNumberFormatter and setMaximumFractionDigits, then:

    [numberFormatter stringFromNumber:myNumber]
    

    Thanks to everyone especially @falconcreek