2011-03-14 44 views
2

我想显示一个数字值到最大小数位数。如果您没有将浮动格式设置为:Xcode小数位数

@"%.1f" 

然后它将显示数字,例如: 1.000000。我想要的是该号码将具有所需的最大小数位数,例如,

1不需要任何

1.5将需要1位小数

1.24将需要2位小数

是否有某种代码格式数位小数的最大数量?

回答

4

将“f”替换为“g”。

printf(3)

gG  The double argument is converted in style f or e (or F or E for G conver- 
     sions). The precision specifies the number of significant digits. If the 
     precision is missing, 6 digits are given; if the precision is zero, it is 
     treated as 1. Style e is used if the exponent from its conversion is less 
     than -4 or greater than or equal to the precision. Trailing zeros are removed 
     from the fractional part of the result; a decimal point appears only if it is 
     followed by at least one digit. 
+0

辉煌谢谢:)那么简单:d – 2011-03-14 17:09:16