Android 如何使用十进制符号和货币格式化double?

Android 如何使用十进制符号和货币格式化double?,android,formatting,double,decimal,currency,Android,Formatting,Double,Decimal,Currency,我的结果应该是这样的:$100,99 我设法按需要格式化了数字,但没有货币。 我设法分别获得货币,但无法将两者结合在一起。 对于编号格式,我使用了DecimalFormatSymbol,就像在回答这个问题时一样 对于我使用此代码的货币: fun getFormattedCurrency(currency: String, amount: Double): String { val c = Currency.getInstance(currency) val nf = Numb

我的结果应该是这样的:$100,99 我设法按需要格式化了数字,但没有货币。 我设法分别获得货币,但无法将两者结合在一起。 对于编号格式,我使用了
DecimalFormatSymbol
,就像在回答这个问题时一样

对于我使用此代码的货币:

fun getFormattedCurrency(currency: String, amount: Double): String {
     val c = Currency.getInstance(currency)
     val nf = NumberFormat.getCurrencyInstance()
     nf.currency = c
     return  nf.format(amount)
}
如何将两者结合起来?

希望这对您有所帮助

    val decimalFormatSymbols = DecimalFormatSymbols().apply {
        decimalSeparator = ','
        groupingSeparator = ' '
        setCurrency(Currency.getInstance("AED"))
    }


    val decimalFormat = DecimalFormat("$ #,###.00", decimalFormatSymbols)
    val text = decimalFormat.format(2333222)
    println(text) //$ 2 333 222,00


    val decimalFormat2 = DecimalFormat("¤ #,###.00", decimalFormatSymbols)
    val text2 = decimalFormat2.format(2333222)
    println(text2) //AED 2 333 222.00
通知如果您使用·,而不是特定的货币符号,如$,则欧元将根据您创建的货币实例使用符号。您还可以从文档中获取更多信息。

您还可以在中找到ISO 4217代码

如果在末尾添加货币符号,不是会更简单吗?如果您在某些文本视图中显示金额,只需执行类似于
myTextView.text=currency+amount.toString()
的操作,也不要忘记货币是由LocaleThanks格式化的!快到了。。。text2没有逗号作为分隔符,尽管text有。在单据上看到:
货币符号;以货币符号代替;如果翻倍,则由国际货币符号代替;如果模式中存在货币小数点分隔符,则使用货币小数点分隔符而不是小数点分隔符。
是否要绕过它?很高兴听到它有帮助:)
    val decimalFormatSymbols = DecimalFormatSymbols().apply {
        decimalSeparator = ','
        groupingSeparator = ' '
        setCurrency(Currency.getInstance("AED"))
    }


    val decimalFormat = DecimalFormat("$ #,###.00", decimalFormatSymbols)
    val text = decimalFormat.format(2333222)
    println(text) //$ 2 333 222,00


    val decimalFormat2 = DecimalFormat("¤ #,###.00", decimalFormatSymbols)
    val text2 = decimalFormat2.format(2333222)
    println(text2) //AED 2 333 222.00