You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android V15更新后货币格式小数被截断问题求助

Android V15更新后货币格式小数被截断问题求助

各位Android开发的大佬们,我最近遇到个头疼的问题,想请大家帮忙支支招!

我在Android Studio里用Kotlin写了个货币格式化函数,用来把任意类型的数值转换成带两位小数的南非兰特(ZAR)格式,之前在Android V14及以下版本都运行正常,结果把系统更到V15之后,发现小数部分直接被截断了!比如输入123.45,之前会显示R 123.45,现在变成R 123,连.00都没了,完全不符合需求。

先贴一下我的代码:

fun formatCurrencyWithDecimalsRefactored(amount: Any?): String {
    // TODO: Decouple logic
    val locale = Locale("en", "ZA")
    Locale.setDefault(locale)
    val symbol = Currency.getInstance(locale).getSymbol(locale)

    val fmt = DecimalFormat("#,###,##0.00")
    fmt.isDecimalSeparatorAlwaysShown = true
    fmt.positivePrefix = "$symbol "
    fmt.negativePrefix = "$symbol -"
    
    return try {
        val number = when(amount) {
            is Number -> amount.toDouble()
            is String -> amount.toDoubleOrNull() ?: 0.0
            else -> 0.0
        }
        fmt.format(number)
    } catch (e: Exception) {
        "$symbol 0.00"
    }
}

我已经试过几种方法排查,但都没解决:

  • 调整DecimalFormat的模式,比如改成"#,##0.00",结果还是一样截断小数
  • 检查了Currency.getInstance(locale)返回的货币对象,它的默认小数位数还是2,和之前版本一致
  • 尝试去掉Locale.setDefault(locale),直接用指定locale初始化DecimalFormat,问题依旧

有没有大佬遇到过Android V15对DecimalFormat或者货币格式化逻辑的改动?或者有没有更稳定的替代方案,能不受系统版本影响,保证始终显示两位小数的货币格式?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 13:53:04