Joda money:如何将格式化的字符串转换回货币值(与区域设置无关)



假设我们的金额为12345.67美元

这可以使用joda货币库转换为货币价值,带有:

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)*/

问题是我如何将本地化的String解析回货币值(注意分组数字是一个点,带的小数是一个逗号(。这还必须处理来自上面格式化程序的每个money值和每个字符串。

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());

最新更新