I'm a complete novice to objective C/ipad development, but I am t开发者_Python百科rying to build a simple four function calculator on the iPad.
My problem is trying to display a number with a decimal in it: i.e. 5.273 or .0021
My entire calculator is float, but when doing
[resultfield setText: NSString stringWithFormat:@"%lf", display]]
the output (say the one button was pressed) would be: 1.00000. I know that if you change the format to @".0lf" all the extra zeroes will disappear and the output would be 1
However, that doesn't help me when doing trying to input a decimal number, or when doing division. If I divide 10/3 the result would be displayed as 3 rather than 3.333333333
Any ideas on how to keep my numbers as float but not display trailing zeroes?
Check out the NSNumberFormatter class. It will let you set rounding and truncation of your decimals.
精彩评论