NSDecimalNumber 小数点后加零(iPhone SDK)

发布于 2024-08-15 12:03:53 字数 1180 浏览 6 评论 0原文

我有以下代码,

(...)
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫弦 2024-08-22 12:03:53

NSDecimalNumber 是 NSNumber 的子类,因此您只需要使用 NSNumberFormatter


 NSDecimalNumber * dn = [NSDecimalNumber decimalNumberWithString:@"1234.5"];
 NSNumberFormatter * nf = [[NSNumberFormatter alloc] init];
 [nf setMinimumFractionDigits:2];
 [nf setMaximumFractionDigits:2];
 NSString *ns  = [nf stringFromNumber:dn];

Simples

NSDecimalNumber subclasses NSNumber so you just need to use NSNumberFormatter


 NSDecimalNumber * dn = [NSDecimalNumber decimalNumberWithString:@"1234.5"];
 NSNumberFormatter * nf = [[NSNumberFormatter alloc] init];
 [nf setMinimumFractionDigits:2];
 [nf setMaximumFractionDigits:2];
 NSString *ns  = [nf stringFromNumber:dn];

Simples

软糖 2024-08-22 12:03:53

为什么不使用 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文