如何将NSDecimalNumber转换为double

[英]How to convert NSDecimalNumber to double


I have a value being stored as an NSDecimalNumber and when I convert it to a double it's losing precision.

我有一个值存储为NSDecimalNumber,当我将其转换为double时,它会失去精度。

For the current piece of data I'm debugging against, the value is 0.2676655. When I send it a doubleValue message, I get 0.267665. It's truncating instead of rounding and this is wreaking havoc with some code that uses hashes to detect data changes for a syncing operation.

对于我正在调试的当前数据,值为0.2676655。当我发送一个doubleValue消息时,我得到0.267665。它正在截断而不是舍入,这会对使用哈希检测同步操作的数据更改的一些代码造成严重破坏。

The NSDecimalNumber instance comes from a third-party framework so I can't just replace it with a primitive double. Ultimately it gets inserted into an NSMutableString so I'm after a string representation, however it needs to be passed through a format specifier of "%.6lf", basically I need six digits after the decimal so it looks like 0.267666.

NSDecimalNumber实例来自第三方框架,所以我不能用原始double替换它。最终它被插入一个NSMutableString所以我在一个字符串表示后,但它需要通过格式说明符“%。6lf”传递,基本上我需要十进制后的六位数,所以它看起来像0.267666。

How can I accomplish this without losing precision? If there's a good way to format the NSDecimalNumber without converting to a double that will work as well.

如何在不失精度的情况下实现这一目标?如果有一种很好的方法来格式化NSDecimalNumber而不转换为可以正常工作的double。

2 个解决方案

#1


4  

The NSDecimalNumber instance comes from a third-party framework so I can't just replace it with a primitive double.

NSDecimalNumber实例来自第三方框架,所以我不能用原始double替换它。

Yes you can. NSDecimalNumber is an immutable subclass of NSNumber, which is a little too helpful when it comes to conversion:

是的你可以。 NSDecimalNumber是NSNumber的不可变子类,在转换时有点太有帮助:

double myDub = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithDouble:((double)0.2676655)] doubleValue]];

Ultimately it gets inserted into an NSMutableString so I'm after a string representation, however it needs to be passed through a format specifier of "%.6lf", basically I need six digits after the decimal so it looks like 0.267666.

最终它被插入一个NSMutableString所以我在一个字符串表示后,但它需要通过格式说明符“%。6lf”传递,基本上我需要十进制后的六位数,所以它看起来像0.267666。

Double precision unfortunately does not round, but getting a string value that's off by one-millionth is not that big of a deal (I hope):

不幸的是,双精度不会变圆,但是获得一个减去百万分之一的字符串值并不是一件大事(我希望):

NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithDouble:((double)0.2676655)] decimalValue]];
NSString *numString = [NSString stringWithFormat:@"%.6lf", [num doubleValue]];
NSLog(@"%@",numString);

#2


2  

I think that your are on a wrong path and somewhere lost in what to do.

我认为你走错了路,在某个地方迷失了。

First of all, keep in mind that in objective-c lond double is not supported, so you might better want to use something like %f instead of %lf. [to be found in the documentation library under "Type encodings" of the objective c runtime programming guide]

首先,请记住,在objective-c lond double中不支持,所以你可能最好使用%f而不是%lf。 [可在目标c运行时编程指南的“类型编码”下的文档库中找到]

Then I would rather expect that the value is show as being truncated, as the doubleValue returns an approximate value but the range you are using is still within the correct range.

然后我宁愿期望值显示为被截断,因为doubleValue返回一个近似值,但您使用的范围仍然在正确的范围内。

You should use a simple formatter instead of moving numbers around, like:

您应该使用简单的格式化程序而不是移动数字,例如:

    // first line as an example for your real value
    NSDecimalNumber *value = [NSDecimalNumber decimalNumberWithString:@"0.2676655"];
    NSNumberFormatter *numFmt = [[NSNumberFormatter alloc] init];
    [numFmt setMaximumFractionDigits:6];
    [numFmt setMinimumFractionDigits:6];
    [numFmt setMinimumIntegerDigits:1];
    NSLog(@"Formatted number %@",[numFmt stringFromNumber:value]);

This has another benefit of using a locale aware formatter if desired. The result of the number formatter is the desired string.

如果需要,这具有使用区域设置感知格式化程序的另一个好处。数字格式化程序的结果是所需的字符串。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2012/07/18/720659c7a6db973ede96fa330caad352.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告