本文介紹了Objective-C如何打印出浮點數(shù)的前導(dǎo)0?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
如何使用 NSString
類型為浮點數(shù)打印前導(dǎo)零?
How do you print out leading zeros for a float using the NSString
type?
Input: 3.14
Desired output: 03.1
Using format @"%02.1f"
Output: 3.1
推薦答案
你想要 @"04.1f"
.4 是總寬度.
正如您從 文檔,格式字符串符合IEEE printf 規(guī)范.
As you can see from the documentation, the format strings conform to the IEEE printf specification.
您指定的格式字符串分解如下:
The format string you've specified breaks down as follows:
0 -- 用零填充.
2 -- 整個生成的格式化值的最小寬度為 2.
.1 -- 小數(shù)點后一位精度.
0 -- Pad with zeros.
2 -- The entire resulting formatted value will have a minimum width of 2.
.1 -- Precision of 1 digit following the decimal point.
這篇關(guān)于Objective-C如何打印出浮點數(shù)的前導(dǎo)0?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!