string - Formatting floats in Objective C -
i need format float (catchy title, he?) 2 decimal places, if decimal places have values aren't zero. example:
i have nstextfield named 'answer', after math couple of floats, want assign 'answerfloat' variable 'answer' nstextfield. far i've got:
[answer setstringvalue:[nsstring stringwithformat:@"%.2f", answerfloat]];
but sets 45 45.00. want whole numbers displayed without zeroes, , decimal numbers displayed respective decimal values.
do need run kind of check before giving stringwithformat? or nsstring offer way handle this?
have tried %g
format specifier?
nslog([nsstring stringwithformat:@"%g, %g", 45.0, 45.5]);
2010-01-12 19:54:38.651 foo[89884:10b] 45, 45.5
Comments
Post a Comment