簡體   English   中英

NSNumberFormatter問題

[英]NSNumberFormatter question

我試圖像這樣保存一個浮動

[NSNumber numberWithFloat:[dosage floatValue]];

並像這樣讀取值:

dosage = [NSString stringWithFormat:@"%1.0f", [prop.dosage floatValue]];

問題是像0.11這樣的數字最終為0

我讀過我可以使用NSNumberFormatter ,但是文檔中關於如何設置它的取整尚不十分清楚。

如果它已經是一個字符串,那么您是否需要實現stringWithFormat

對於2位浮點值,請使用%.2f

dosage = [NSString stringWithFormat:@"%0.2f", [prop.dosage floatValue]];

快樂編碼...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM