简体   繁体   English

NSNumberFormatter问题

[英]NSNumberFormatter question

I'm trying to save a float like so 我试图像这样保存一个浮动

[NSNumber numberWithFloat:[dosage floatValue]];

and read the value like so: 并像这样读取值:

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

The Problem is that numbers like 0.11 end up being 0 问题是像0.11这样的数字最终为0

I've read that I can use NSNumberFormatter , but the docs are not very clear on how to set it for rounding. 我读过我可以使用NSNumberFormatter ,但是文档中关于如何设置它的取整尚不十分清楚。

if its already a string then y do you need to implement stringWithFormat ? 如果它已经是一个字符串,那么您是否需要实现stringWithFormat

for 2 digits floatvalue use %.2f ie 对于2位浮点值,请使用%.2f

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

HAPPY iCODING... 快乐编码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM