2011-03-14 53 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