java-MoneyFormatter :: parseMoney引发MoneyFormatException
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-MoneyFormatter :: parseMoney引发MoneyFormatException,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5463字,纯文字阅读大概需要8分钟。
内容图文
![java-MoneyFormatter :: parseMoney引发MoneyFormatException](/upload/InfoBanner/zyjiaocheng/660/574f652c44fa4df4ac4b683364cb0bba.jpg)
采取以下SSCCE(已安装Joda-Money库):
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("100");
MoneyFormatter mf = new MoneyFormatterBuilder().appendLiteral("$").appendAmount(MoneyAmountStyle.LOCALIZED_GROUPING).toFormatter();
String money_as_string = mf.print(Money.of(CurrencyUnit.USD, bd)); // The MoneyFormatter is what printed this string...
System.out.println(money_as_string);
Money money = mf.parseMoney(money_as_string); // You think it should be able to parse the string it gave me, right?
}
我使用了MoneyFormatter来打印字符串:money_as_string.我的(合理的?)期望是,我可以使用相同的MoneyFormatter将字符串解析回Money对象.但是,a,没有骰子.它引发此错误:
Exception in thread "main" org.joda.money.format.MoneyFormatException: Parsing did not find both currency and amount: $100.00
at org.joda.money.format.MoneyFormatter.parseBigMoney(MoneyFormatter.java:237)
at org.joda.money.format.MoneyFormatter.parseMoney(MoneyFormatter.java:258)
at test4.Test12.main(Test12.java:35)
因此,我的问题是:如何从字符串中准确地获取Money对象?
编辑:@ durron597,您的信息很有帮助,但未回答问题.那么,您究竟如何从字符串变成Money对象?
我添加了删除美元符号的代码:
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("100");
MoneyFormatter mf = new MoneyFormatterBuilder().appendLiteral("$").appendAmount(MoneyAmountStyle.LOCALIZED_GROUPING).toFormatter();
String money_as_string = mf.print(Money.of(CurrencyUnit.USD, bd)); // The MoneyFormatter is what printed this string...
System.out.println(money_as_string);
Money money = mf.parseMoney(money_as_string.replace("$", "").trim()); // You think it should be able to parse the string it gave me, right?
}
我得到了这个:
Exception in thread "main" org.joda.money.format.MoneyFormatException: Text could not be parsed at index 0: 100.00
at org.joda.money.format.MoneyFormatter.parseBigMoney(MoneyFormatter.java:233)
at org.joda.money.format.MoneyFormatter.parseMoney(MoneyFormatter.java:258)
at test4.Test12.main(Test12.java:35)
因此,它无法解析带有或不带有货币符号的文本.
任何人都可以让parseMoney()函数正常工作吗?
解决方法:
因此,对我来说突出的第一件事是,您使用的是appendLiteral而不是appendCurrencySymbolLocalized(如果使用的是CurrencyUnit.EUR,那会是$.)
但是,当您将其更改为此:
MoneyFormatter mf = new MoneyFormatterBuilder()
.appendCurrencySymbolLocalized()
.appendAmount(MoneyAmountStyle.LOCALIZED_GROUPING)
.toFormatter();
您的代码将引发以下异常:
Exception in thread "main" java.lang.UnsupportedOperationException: MoneyFomatter has not been configured to be able to parse
at org.joda.money.format.MoneyFormatter.parse(MoneyFormatter.java:281)
at org.joda.money.format.MoneyFormatter.parseBigMoney(MoneyFormatter.java:229)
at org.joda.money.format.MoneyFormatter.parseMoney(MoneyFormatter.java:258)
at MoneyTest.main(MoneyTest.java:17)
进一步检查发现这条告诉线in the javadoc:
appendCurrencySymbolLocalized
public
0700107002Appends the localized currency symbol to the builder.
The localized currency symbol is the symbol as chosen by the locale of the formatter.Symbols cannot be parsed.
Returns: this, for chaining, never null
为什么会这样呢?可能是因为货币符号不明确. Many countries use a dollar.以下是一些examples:
In addition to those countries of the world that use dollars or pesos, a number of other countries use the $symbol to denote their currencies, including:
- Nicaraguan córdoba (usually written as C$)
- Samoan tālā (a transliteration of the word dollar)
- Tongan pa?anga
- Zimbabwe (usually written as Z$)
试试这个代码:
MoneyFormatter mf = new MoneyFormatterBuilder()
.appendCurrencyCode()
.appendAmount(MoneyAmountStyle.LOCALIZED_GROUPING)
.toFormatter();
这样可以打印出100.00美元之类的东西,并且可以正确解析.如果您绝对必须带有美元符号,则需要实现自己的打印机/解析器.或者,您可以使用此代码:
import java.io.IOException;
import java.math.BigDecimal;
import org.joda.money.BigMoney;
import org.joda.money.CurrencyUnit;
import org.joda.money.IllegalCurrencyException;
import org.joda.money.Money;
import org.joda.money.format.MoneyAmountStyle;
import org.joda.money.format.MoneyFormatter;
import org.joda.money.format.MoneyFormatterBuilder;
import org.joda.money.format.MoneyParseContext;
import org.joda.money.format.MoneyParser;
import org.joda.money.format.MoneyPrintContext;
import org.joda.money.format.MoneyPrinter;
public class MoneyTest {
private static enum DollarParserPrinter implements MoneyParser,
MoneyPrinter {
INSTANCE;
private static final String DOLLAR_SYMBOL = "$";
@Override
public void parse(MoneyParseContext context) {
int endPos = context.getIndex() + 2;
if (endPos > context.getTextLength()) {
context.setError();
} else {
String code =
context.getTextSubstring(context.getIndex(), endPos);
if(DOLLAR_SYMBOL.equals(code)) {
context.setCurrency(CurrencyUnit.USD);
context.setIndex(endPos);
}
}
}
@Override
public void print(MoneyPrintContext context, Appendable appendable,
BigMoney money) throws IOException {
if(CurrencyUnit.USD == money.getCurrencyUnit()) {
appendable.append(DOLLAR_SYMBOL);
} else {
throw new IllegalCurrencyException("This parser only knows how to print US Dollar money!");
}
}
}
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("100");
MoneyFormatter mf =
new MoneyFormatterBuilder().append(DollarParserPrinter.INSTANCE, DollarParserPrinter.INSTANCE)
.appendAmount(MoneyAmountStyle.LOCALIZED_GROUPING)
.toFormatter();
String money_as_string = mf.print(Money.of(CurrencyUnit.USD, bd));
System.out.println(money_as_string);
Money money = mf.parseMoney(money_as_string);
System.out.println(money);
}
}
内容总结
以上是互联网集市为您收集整理的java-MoneyFormatter :: parseMoney引发MoneyFormatException全部内容,希望文章能够帮你解决java-MoneyFormatter :: parseMoney引发MoneyFormatException所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。