Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Swift datatype do I use for currency

Tags:

ios

swift

I have an iOS application that will be performing a lot of basic arithmetic on numbers representing USD currency (eg 25.00 representing $25.00).

I have gotten into a lot of trouble using the datatype Double in other languages like Java and Javascript so I would like to know the best datatype to use for currency in Swift.

like image 229
PeonProgrammer Avatar asked Feb 09 '15 23:02

PeonProgrammer


People also ask

What data type is currency?

Currency variables are stored as 64-bit (8-byte) numbers in an integer format, scaled by 10,000 to give a fixed-point number with 15 digits to the left of the decimal point and 4 digits to the right.

Which data type should you use to store values in your application to represent currency?

The best datatype to use for currency in C# is decimal. The decimal type is a 128-bit data type suitable for financial and monetary calculations. The decimal type can represent values ranging from 1.0 * 10^-28 to approximately 7.9 * 10^28 with 28-29 significant digits.

Can we use double for money?

Float and double are bad for financial (even for military use) world, never use them for monetary calculations.

What would be a good data types to store prices for an international shopping website?

The best type for price column should be DECIMAL. The type DECIMAL stores the value precisely. For Example - DECIMAL(10,2) can be used to store price value. It means the total digit will be 10 and two digits will be after decimal point.


2 Answers

Use Decimal, and make sure you initialize it properly!

CORRECT

 // Initialising a Decimal from a Double: let monetaryAmountAsDouble = 32.111 let decimal: Decimal = NSNumber(floatLiteral: 32.111).decimalValue print(decimal) // 32.111  😀 let result = decimal / 2 print(result) // 16.0555 😀   // Initialising a Decimal from a String: let monetaryAmountAsString = "32,111.01"  let formatter = NumberFormatter() formatter.locale = Locale(identifier: "en_US") formatter.numberStyle = .decimal  if let number = formatter.number(from: monetaryAmountAsString) {     let decimal = number.decimalValue     print(decimal) // 32111.01 😀     let result = decimal / 2.1     print(result) // 15290.9571428571428571428571428571428571 😀 } 

INCORRECT

let monetaryAmountAsDouble = 32.111 let decimal = Decimal(monetaryAmountAsDouble)  print(decimal) // 32.11099999999999488  😟  let monetaryAmountAsString = "32,111.01" if let decimal = Decimal(string: monetaryAmountAsString, locale: Locale(identifier: "en_US")) {     print(decimal) // 32  😟 } 

Performing arithmetic operations on Doubles or Floats representing currency amounts will produce inaccurate results. This is because the Double and Float types cannot accurately represent most decimal numbers. More information here.

Bottom line: Perform arithmetic operations on currency amounts using Decimals or Int

like image 187
Eric Avatar answered Oct 13 '22 00:10

Eric


I suggest you start with a typealias for Decimal. Example:

typealias Dollars = Decimal let a = Dollars(123456) let b = Dollars(1000) let c = a / b print(c) 

Output:

123.456 

If you are trying to parse a monetary value from a string, use a NumberFormatter and set its generatesDecimalNumbers property to true.

like image 39
rob mayoff Avatar answered Oct 13 '22 00:10

rob mayoff