Last active
July 10, 2024 17:50
-
-
Save Tulakshana/ba78370e2e8e88e1713c3c65e7ac8b0a to your computer and use it in GitHub Desktop.
Different currencies have different standards for minor unit (ISO 4217: https://www.iso.org/iso-4217-currency-codes.html). e.g. USD minor units are displayed with two decimal places. JPY doesn’t have a minor unit. The solution is to store the value in minor units and make it client’s responsibility to do the conversion.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let value = 10000 // Value stored in the database along with the currency code | |
let numberFormatter = NumberFormatter() | |
numberFormatter.numberStyle = .currency | |
numberFormatter.currencyCode = "USD" | |
print("\(Double(value)/pow(10, Double(numberFormatter.maximumFractionDigits)))") // 100.0 | |
numberFormatter.currencyCode = "JPY" | |
print("\(Double(value)/pow(10, Double(numberFormatter.maximumFractionDigits)))") // 10000.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment