NSNumberFormatter 和舍入百分比值

发布于 2024-08-16 02:29:28 字数 583 浏览 5 评论 0原文

这是我的代码片段,我不知道如何舍入双数。

double m =  [tmpProduct.msrp doubleValue] ;
double d = [tmpProduct.discountPrice doubleValue];
double p = (d * 100 ) / m;

这里 tmpProduct.msrp 和 mpProduct.discountPrice 是 (NSDecimalNUmber *)

来自上面的操作我得到 p = 44.995757

我想将其转换为 (45%) ,我该怎么做?

这是我使用的,但这没有帮助。

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundDown];
[formatter stringFromNumber:[NSNumber numberWithDouble:p]]

here is my code snip, i dont know how to round double numbers.

double m =  [tmpProduct.msrp doubleValue] ;
double d = [tmpProduct.discountPrice doubleValue];
double p = (d * 100 ) / m;

here tmpProduct.msrp and mpProduct.discountPrice are (NSDecimalNUmber *)

from the operation above I got p = 44.995757

i want to convert it to (45%) , how do i do that?

here is what i use , but that does not help.

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundDown];
[formatter stringFromNumber:[NSNumber numberWithDouble:p]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

水晶透心 2024-08-23 02:29:28

您是否尝试过使用 NSDecimalNumber 提供的算术运算?
我自己没有尝试过,但似乎您通过在数字实例的双值上使用标准运算符(*、/、...)来绕过 NSDecimalNumber 的优点。
看看 NSDecimalNumber:执行算术

由于 Objective-C 不支持运算符重载,您将不得不使用以下方法:

- (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber

更新:
使用 NSDecimalNumber 进行除法:

NSDecimalNumber* operandOne = [NSDecimalNumber decimalNumberWithString:@"1234.5"];
NSDecimalNumber* operandTwo = [NSDecimalNumber decimalNumberWithString:@"2335"];
NSDecimalNumber* result = [operandOne decimalNumberByDividingBy:operandTwo];
NSLog(@"%@", [result descriptionWithLocale:[NSLocale currentLocale]]);

通过使用 descriptionWithLocale: ,您将得到正确的 NSLocaleDecimalSeparator

更新 2:
我忘记了四舍五入:
NSDecimalNumber 支持多种舍入模式。
阅读 decimalNumberByRoundingAccordingToBehavior:

Have you tried using the arithmetic operations NSDecimalNumber provides?
I haven't tried that myself, but it seems you are bypassing the advantages of NSDecimalNumber by using standard operators ( *, /, ...) on the double values of your number instances.
Take a look at NSDecimalNumber: Performing Arithmetic

As Objective-C does not support operator overloading you will have to use methods such as:

- (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber

Update:
Division using NSDecimalNumber:

NSDecimalNumber* operandOne = [NSDecimalNumber decimalNumberWithString:@"1234.5"];
NSDecimalNumber* operandTwo = [NSDecimalNumber decimalNumberWithString:@"2335"];
NSDecimalNumber* result = [operandOne decimalNumberByDividingBy:operandTwo];
NSLog(@"%@", [result descriptionWithLocale:[NSLocale currentLocale]]);

by using descriptionWithLocale: , you get the correct NSLocaleDecimalSeparator

Update 2:
I forgot about the rounding:
NSDecimalNumber supports several rounding modes.
Read the documentation of decimalNumberByRoundingAccordingToBehavior:

俯瞰星空 2024-08-23 02:29:28

在这种特定情况下,我建议将样式设置为 NSNumberFormatterPercentStyle 并将最大小数位数设置为 0。这样您就不必担心舍入问题。这对我有用:

double p = d / m;  // note: not multiplying by 100 here

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterPercentStyle];
NSString *s = [formatter stringFromNumber:[NSNumber numberWithDouble:p]];

这给出了字符串“45%”。

In this specific case, I'd suggest setting the style to NSNumberFormatterPercentStyle and the maximum fraction digits to 0. Then you shouldn't have to worry about the rounding. This works for me:

double p = d / m;  // note: not multiplying by 100 here

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterPercentStyle];
NSString *s = [formatter stringFromNumber:[NSNumber numberWithDouble:p]];

This gives the string "45%".

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