I have an issue where I need to have result with different precisions maximum one is up to 18 digits after coma.
Because this operation contains money I decided to use Decimal
type. So I have questions.
Is
Decimal
data type suitable to represent values up to 18 digits after coma?Will I have different behavior on older phones such as iPhone 5 with that data type?
Are any corner cases when usage of the
Decimal
andNumberFormatter()
withmaximumFractionDigits
= 18 ? (Wrong rounding .. etc)
This is my case with example usage:
I have price for coin in USD:
let pricePerCoin: Decimal = 950.83
Than I have amount in USD that I will use to buy coins.
let amountInUSD: Decimal = 10000
I use formatter to be able to convert Decimal
number to string with respect of the maximumFractionDigits
.
let fractionDigits = 18
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.groupingSeparator = ""
formatter.isLenient = true
formatter.maximumFractionDigits = fractionDigits
Result
let result = formatter.string(for: amountInUSD/pricePerCoin) // 10.517127141550011933