Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDecimalNumber to double. What is the correct way to do this in Swift [closed]

Tags:

ios

swift

I've been searching for a clean and correct way to convert an NSDecimalNumber retrieved from CoreData to a Double.

I'm looking for a recommendation on how to do this. I need to take 2 decimal numbers from my CoreData and calculate a percentage to use in a progress bar etc.

like image 426
ddpishere Avatar asked Feb 01 '15 22:02

ddpishere


People also ask

What is double Swift?

Floating-Point Numbers Swift provides two signed floating-point number types: Double represents a 64-bit floating-point number. Float represents a 32-bit floating-point number.

What is NSDecimalNumber?

NSDecimalNumber , an immutable subclass of NSNumber , provides an object-oriented wrapper for doing base-10 arithmetic. An instance can represent any number that can be expressed as mantissa x 10^exponent where mantissa is a decimal integer up to 38 digits long, and exponent is an integer from –128 through 127.


1 Answers

NSDecimalNumber has a property var doubleValue: Double { get }. So you can say num.doubleValue.

For division of two NSDecimalNumbers you should use num1.dividing(by: num2).

Keep in mind that in Swift you can also use Decimal which supports +, *, /, etc.

like image 181
jtbandes Avatar answered Nov 15 '22 16:11

jtbandes