NSDecimalNumber 小数点后加零(iPhone SDK)
我有以下代码,
(...)
numberStyle = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain
scale:2
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:NO];
strThree = textFieldThree.text;
strThree = [strThree stringByReplacingOccurrencesOfString:@"." withString:@""];
fieldOneDNa = (NSDecimalNumber *)[NSDecimalNumber decimalNumberWithString:strThree];
oneHundred = (NSDecimalNumber *)[NSDecimalNumber decimalNumberWithString:@"100"];
fieldOneDN = [fieldOneDNa decimalNumberByDividingBy:oneHundred
withBehavior:numberStyle];
firstStepReturn = [fieldOneDN stringValue];
textFieldThree.text = firstStepReturn;
(...)
我希望能够让firstStepReturn 在小数点后有 2 位数字,即使它们是 .00 或 .*0。例如,如果在文本字段“textFieldThree”中输入 323.390,则它将被处理并作为 3233.90 返回到文本字段。目前显示为3233.9。
任何帮助将不胜感激。
I have the following code
(...)
numberStyle = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain
scale:2
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:NO];
strThree = textFieldThree.text;
strThree = [strThree stringByReplacingOccurrencesOfString:@"." withString:@""];
fieldOneDNa = (NSDecimalNumber *)[NSDecimalNumber decimalNumberWithString:strThree];
oneHundred = (NSDecimalNumber *)[NSDecimalNumber decimalNumberWithString:@"100"];
fieldOneDN = [fieldOneDNa decimalNumberByDividingBy:oneHundred
withBehavior:numberStyle];
firstStepReturn = [fieldOneDN stringValue];
textFieldThree.text = firstStepReturn;
(...)
I would like to be able to have firstStepReturn have 2 digits following the decimal point even if they are .00 or .*0. For example, if 323.390 was inputted into the textfield "textFieldThree", it would be processed and returned to the textfield as 3233.90. At the moment, it is displaying as 3233.9.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSDecimalNumber 是 NSNumber 的子类,因此您只需要使用 NSNumberFormatter
Simples
NSDecimalNumber subclasses NSNumber so you just need to use NSNumberFormatter
Simples
为什么不使用
textFieldThree.text=[NSString stringWithFormat:@"%.2f",[fieldOneDN floatvalue]];
(或类似的东西 - 抱歉我现在在电脑上,所以无法测试这)
Why not use
textFieldThree.text=[NSString stringWithFormat:@"%.2f",[fieldOneDN floatvalue]];
(or something similar - sorry I'm on a PC right now so can't test this)