Modern Apps git

← Modern-Apps.git

travel: pin formatMoney decimals to Locale.US

Commit
3329c6eb2fad5b5c661b8f07f272867e43889c2a
Author
Vayun Mathur <[email protected]>
When
2026-11-08 02:37 UTC (28 days ago)
Parents
05b6fca
Fractional booking amounts were formatted with String.format's default locale, so on devices set to a comma-decimal locale (de-DE, fr-FR, ...) a price like 220.50 rendered as "220,50" (e.g. "€220,50"). Whole amounts were unaffected (integer path), and parsing was never affected (toDoubleOrNull is locale-independent), so this only showed on fractional totals.

Root cause: "%.2f".format(value) in ui/TravelFormat.kt picks up Locale.getDefault(). The fix passes Locale.US explicitly, matching the decimal-point amounts the server sends.

User-visible effect: money labels now always render with a decimal point regardless of device locale.

Verified with :travel:testDevUnitTest (all green), including a new regression test that sets the default locale to GERMANY and asserts formatMoney("220.5", "EUR") == "€220.50".

Diff

travel/src/main/java/com/vayunmathur/travel/ui/TravelFormat.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/ui/TravelFormat.kt b/travel/src/main/java/com/vayunmathur/travel/ui/TravelFormat.ktindex 998a35dfbe..758c3dc67c 100644--- a/travel/src/main/java/com/vayunmathur/travel/ui/TravelFormat.kt+++ b/travel/src/main/java/com/vayunmathur/travel/ui/TravelFormat.kt@@ -1,5 +1,7 @@ package com.vayunmathur.travel.ui +import java.util.Locale+ /**  * Short fare-condition chips for an offer, e.g. ["Refundable", "Changeable"].  * Falls back to "Non-refundable" when a refund rule is explicitly disallowed.@@ -25,7 +27,9 @@ fun formatMoney(amount: String, currency: String): String {         "GBP" -> "£"         else -> currency.uppercase() + " "     }-    return symbol + (if (value % 1.0 == 0.0) value.toInt().toString() else "%.2f".format(value))+    // Pin the decimal separator: the device locale may use ',' (e.g. de-DE),+    // while booking amounts are always decimal-point strings.+    return symbol + (if (value % 1.0 == 0.0) value.toInt().toString() else "%.2f".format(Locale.US, value)) }  /** "510" minutes -> "8h 30m". */
travel/src/test/java/com/vayunmathur/travel/pure/TravelFormatTest.kt
diff --git a/travel/src/test/java/com/vayunmathur/travel/pure/TravelFormatTest.kt b/travel/src/test/java/com/vayunmathur/travel/pure/TravelFormatTest.ktindex 907c4291c2..39fd906ada 100644--- a/travel/src/test/java/com/vayunmathur/travel/pure/TravelFormatTest.kt+++ b/travel/src/test/java/com/vayunmathur/travel/pure/TravelFormatTest.kt@@ -44,6 +44,19 @@ class TravelFormatTest {         assertEquals("JPY 1000", formatMoney("1000", "jpy"))     } +    @Test+    fun fractionalAmountsIgnoreDefaultLocale() {+        val previous = java.util.Locale.getDefault()+        try {+            // Comma-decimal locale: the label must still use a decimal point.+            java.util.Locale.setDefault(java.util.Locale.GERMANY)+            assertEquals("€220.50", formatMoney("220.5", "EUR"))+            assertEquals("$412.50", formatMoney("412.5", "USD"))+        } finally {+            java.util.Locale.setDefault(previous)+        }+    }+     @Test     fun unparseableAmountFallsBackToRaw() {         assertEquals("USD abc", formatMoney("abc", "USD"))