如何將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.

如果需要,這具有使用區域設置感知格式化程序的另一個好處。數字格式化程序的結果是所需的字符串。


注意!

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



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