假设我们有金额 $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]*