Joda money:如何将格式化字符串转换回货币值(独立于语言环境)
Joda money: How to convert formatted string back to monetary value (independent of locale)
假设我们有金额 $12345.67
可以使用 joda money 库将其转换为货币价值:
Money parsed = Money.parse("USD 12345.67");
//Now to display the amount according to the users locale we use:
MoneyFormatter mf = MoneyFormatterBuilder().appendAmountLocalized().toFormatter();
String localeString = mf.withLocale(Locale.getDefault()).print(parsed);
//Now assume the user is for example in Europe so the locale string would be 12.345,46
//Now assume he changes the amount inside a textfield to 12.345,47
Money changed = /*parse 12.345,47 (USD)*/
问题是如何将本地化字符串解析回货币值(注意分组数字是一个点,小数数字是逗号)。这也必须与上述格式化程序产生的每个货币值和每个字符串一起使用。
Money.parse()
仅适用于不满足要求的正则表达式 [+-]?[0-9]*[.]?[0-9]*
好的。知道了。您可以使用 MoneyFormatter,反之亦然。文档有点不清楚
MoneyFormatter mf = MoneyFormatterBuilder().appendAmountLocalized().toFormatter()
MoneyParseContext parsed = mf.withLocale(Locale.getDefault()).parse(amount,0);
parsed.setCurrency(currency);
Money money = Money.of(parsed.toBigMoney());
假设我们有金额 $12345.67
可以使用 joda money 库将其转换为货币价值:
Money parsed = Money.parse("USD 12345.67");
//Now to display the amount according to the users locale we use:
MoneyFormatter mf = MoneyFormatterBuilder().appendAmountLocalized().toFormatter();
String localeString = mf.withLocale(Locale.getDefault()).print(parsed);
//Now assume the user is for example in Europe so the locale string would be 12.345,46
//Now assume he changes the amount inside a textfield to 12.345,47
Money changed = /*parse 12.345,47 (USD)*/
问题是如何将本地化字符串解析回货币值(注意分组数字是一个点,小数数字是逗号)。这也必须与上述格式化程序产生的每个货币值和每个字符串一起使用。
Money.parse()
仅适用于不满足要求的正则表达式 [+-]?[0-9]*[.]?[0-9]*
好的。知道了。您可以使用 MoneyFormatter,反之亦然。文档有点不清楚
MoneyFormatter mf = MoneyFormatterBuilder().appendAmountLocalized().toFormatter()
MoneyParseContext parsed = mf.withLocale(Locale.getDefault()).parse(amount,0);
parsed.setCurrency(currency);
Money money = Money.of(parsed.toBigMoney());