Skip to content

Instantly share code, notes, and snippets.

@ccampo133
Last active April 24, 2023 07:38
Show Gist options
  • Save ccampo133/e6315a4d2678be394ff62c7897bfaa48 to your computer and use it in GitHub Desktop.
Save ccampo133/e6315a4d2678be394ff62c7897bfaa48 to your computer and use it in GitHub Desktop.
Currency formatting
import java.math.BigDecimal
import java.text.NumberFormat
import java.util.Currency
import java.util.Locale
fun main(args: Array<String>) {
val locales = listOf(
Locale.FRANCE,
Locale.GERMANY,
Locale.UK,
Locale.ITALY,
Locale("es", "ES"), // Spanish - Spain
Locale("en", "ES"), // English - Spain
Locale("es", "MX"), // Spanish - Mexico
Locale("en", "MX"), // English - Mexico
Locale.US,
Locale.JAPAN
)
val value = BigDecimal.valueOf(1234.56)
// ISO 4217 currency codes; see https://en.wikipedia.org/wiki/ISO_4217#X_currencies
val currencies = listOf(
"MXN", // Mexican Peso
"JPY", // Japanese Yen
"USD", // US Dollar
"EUR", // Euro
"GBP" // UK Pound
)
for (currency in currencies) {
println("Currency: $currency")
for (locale in locales) {
println("Locale: $locale\t Formatted value: ${formatCurrency(value, currency, locale)}")
}
println()
}
}
fun formatCurrency(value: BigDecimal, isoCurrencyCode: String, locale: Locale): String {
val currency = Currency.getInstance(isoCurrencyCode)
val currencyFormat = NumberFormat.getCurrencyInstance(locale)
currencyFormat.currency = currency
return currencyFormat.format(value)
}
/*
* OUTPUT:
*
* Currency: MXN
* Locale: fr_FR Formatted value: 1 234,56 MXN
* Locale: de_DE Formatted value: 1.234,56 MXN
* Locale: en_GB Formatted value: MXN1,234.56
* Locale: it_IT Formatted value: MXN 1.234,56
* Locale: es_ES Formatted value: 1.234,56 MXN
* Locale: en_ES Formatted value: MXN1,234.56
* Locale: es_MX Formatted value: $1,234.56
* Locale: en_MX Formatted value: MXN1,234.56
* Locale: en_US Formatted value: MXN1,234.56
* Locale: ja_JP Formatted value: MXN1,235
*
* Currency: JPY
* Locale: fr_FR Formatted value: 1 234,56 JPY
* Locale: de_DE Formatted value: 1.234,56 JPY
* Locale: en_GB Formatted value: JPY1,234.56
* Locale: it_IT Formatted value: JPY 1.234,56
* Locale: es_ES Formatted value: 1.234,56 JPY
* Locale: en_ES Formatted value: JPY1,234.56
* Locale: es_MX Formatted value: JPY1,234.56
* Locale: en_MX Formatted value: JPY1,234.56
* Locale: en_US Formatted value: JPY1,234.56
* Locale: ja_JP Formatted value: ¥1,235
*
* Currency: USD
* Locale: fr_FR Formatted value: 1 234,56 USD
* Locale: de_DE Formatted value: 1.234,56 USD
* Locale: en_GB Formatted value: USD1,234.56
* Locale: it_IT Formatted value: USD 1.234,56
* Locale: es_ES Formatted value: 1.234,56 USD
* Locale: en_ES Formatted value: USD1,234.56
* Locale: es_MX Formatted value: US$1,234.56
* Locale: en_MX Formatted value: USD1,234.56
* Locale: en_US Formatted value: $1,234.56
* Locale: ja_JP Formatted value: USD1,235
*
* Currency: EUR
* Locale: fr_FR Formatted value: 1 234,56 €
* Locale: de_DE Formatted value: 1.234,56 €
* Locale: en_GB Formatted value: €1,234.56
* Locale: it_IT Formatted value: € 1.234,56
* Locale: es_ES Formatted value: 1.234,56 €
* Locale: en_ES Formatted value: EUR1,234.56
* Locale: es_MX Formatted value: EUR1,234.56
* Locale: en_MX Formatted value: EUR1,234.56
* Locale: en_US Formatted value: EUR1,234.56
* Locale: ja_JP Formatted value: EUR1,235
*
* Currency: GBP
* Locale: fr_FR Formatted value: 1 234,56 GBP
* Locale: de_DE Formatted value: 1.234,56 GBP
* Locale: en_GB Formatted value: £1,234.56
* Locale: it_IT Formatted value: GBP 1.234,56
* Locale: es_ES Formatted value: 1.234,56 GBP
* Locale: en_ES Formatted value: GBP1,234.56
* Locale: es_MX Formatted value: GBP1,234.56
* Locale: en_MX Formatted value: GBP1,234.56
* Locale: en_US Formatted value: GBP1,234.56
* Locale: ja_JP Formatted value: GBP1,235
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment