iosnsstringstringwithformat

stringWitFormat using a zero padded float


double knots = currentLocation.speed*1.943844492;
NSString *speed = [NSString stringWithFormat:@"%03.1f", knots];

This results a normal single decimal float without leading zero's. The purpose is to get max 3 leading digits and single decimal.


Solution

  • The 3 is the total length including the decimal point and the decimal digit. Change the 3 to 5 and you should get what you want:

    NSString *speed = [NSString stringWithFormat:@"%05.1f", knots];