Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimals in Swift

Tags:

ios

swift

ios8

I am writing a Swift app and am dealing with decimals in a database (stored in mysql as decimals, with 2 digits. Basically it's sales someone made each day, so generally anything from $0 to $1000, but not millions, and nothing insane in terms of trailing decimals, just always rounded to 2 decimal places).

Referencing this helped me out: How to properly format currency on ios

..But I wanted to just do a quick sanity check here and make sure this strategy is ok. i.e I would use NSDecimal or NSDecimalNumber (is there a preferred swift equivalent??)

What would you all recommend I do when dealing with currency in Swift? I'd like to use the locale-based currency symbol as well. I have a class called Sales that contains the amount in question. What do you recommend the datatype to be?

Apologies if I am coming off lazy, I actually have some ideas on what to do but feel a little overwhelmed at the "right" approach, especially in a locale-sensitive way, and wanted to check in here with you all.

Thanks so much!

like image 999
NullHypothesis Avatar asked Aug 14 '14 00:08

NullHypothesis


People also ask

What is decimal type in Swift?

In Swift, there are two types of floating-point number, both of which are signed. These are the Float type which represents 32-bit floating point numbers with at least 6 decimal digits of precision and the Double type which represents 64-bit floating point numbers at least 15 decimal digits of precision.

How do you round to 2 decimal places in Swift?

By using round(_:) , ceil(_:) , and floor(_:) you can round Double and Float values to any number of decimal places in Swift.

Can int have decimals Swift?

Second, Swift considers decimals to be a wholly different type of data to integers, which means you can't mix them together. After all, integers are always 100% accurate, whereas decimals are not, so Swift won't let you put the two of them together unless you specifically ask for it to happen.

How do you store decimal values?

The significant digits to the left of the decimal and the significant digits to the right of the decimal are stored in separate groups of bytes. At the maximum precision specification, DECIMAL(32,s) data types can store s-1 decimal digits to the right of the decimal point, if s is an odd number.


2 Answers

Update for Swift 3: A Decimal type is now available with built-in support for operators like *, /, +, <, etc. When used in an Any context (passed to Objective-C), it's bridged to NSDecimalNumber.


Old answer:

NSDecimal is not really supported in Swift (it's a weird opaque pointer type), but NSDecimalNumber is — and as in Obj-C, it's the best thing to use for base-ten arithmetic (because it actually does its operations in base ten). NSLocale, NSNumberFormatter and friends all work too and should satisfy your localization needs.

like image 122
jtbandes Avatar answered Oct 02 '22 12:10

jtbandes


Swift 3 now has a Decimal (value) type which is bridged to NSDecimalNumber.

like image 35
Jon Hull Avatar answered Oct 02 '22 11:10

Jon Hull