Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale independent totalPrice #3863

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.stripe.android
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.Currency
import java.util.Locale
import kotlin.math.pow

/**
Expand All @@ -27,7 +28,8 @@ object PayWithGoogleUtils {
for (i in 0 until totalLength) {
builder.append('#')
}
val noDecimalCurrencyFormat = DecimalFormat(builder.toString())
val noDecimalCurrencyFormat =
DecimalFormat(builder.toString(), DecimalFormatSymbols.getInstance(Locale.ROOT))
noDecimalCurrencyFormat.currency = currency
noDecimalCurrencyFormat.isGroupingUsed = false
return noDecimalCurrencyFormat.format(price)
Expand All @@ -49,9 +51,9 @@ object PayWithGoogleUtils {
val modBreak = 10.0.pow(fractionDigits.toDouble())
val decimalPrice = price / modBreak

// No matter the Locale, Android Pay requires a dot for the decimal separator.
val symbolOverride = DecimalFormatSymbols()
symbolOverride.decimalSeparator = '.'
// No matter the Locale, Android Pay requires a dot for the decimal separator, and Arabic
// numbers.
val symbolOverride = DecimalFormatSymbols.getInstance(Locale.ROOT)
val decimalFormat = DecimalFormat(builder.toString(), symbolOverride)
decimalFormat.currency = currency
decimalFormat.isGroupingUsed = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,17 @@ class PayWithGoogleUtilsTest {
val littlePrice = getPriceString(7, Currency.getInstance("CLP"))
assertThat(littlePrice).isEqualTo("7")
}

@Test
fun getPriceString_whenLocaleWithArabicNumerals_returnsExpectedValue() {
Locale.setDefault(Locale.Builder().setLanguage("ar").setRegion("AE").build())
val priceString = getPriceString(100, Currency.getInstance("USD"))
assertThat(priceString).isEqualTo("1.00")

val littlePrice = getPriceString(8, Currency.getInstance("EUR"))
assertThat(littlePrice).isEqualTo("0.08")

val bigPrice = getPriceString(20000000, Currency.getInstance("GBP"))
assertThat(bigPrice).isEqualTo("200000.00")
}
}