swift 2.0 NSDecimalNumber possible discrepency converting to long

68 Views Asked by At

I can't make heads or tails of this. I am using NSDecimalNumber to truncate the fractional portion from a string. This works in most cases, but not apparently in the case of infinite decimals (or just too many). Here is an example:

print(NSDecimalNumber(string: "49.81666666666666666").longLongValue)
print(NSDecimalNumber(string: "49.816666666666666666").longLongValue)
print(NSDecimalNumber(string: "49.8166666666666666666").longLongValue)

The first line prints 49, the second -5, and the last one 0. I know I can use the rounding function to do the same thing, and that is what I will probably use instead, but doesn't this seem odd? I know it isn't just converting the float bit pattern into a long or else the results would be completely different.

0

There are 0 best solutions below