以这个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打印String:money_as_string。我的(合理?)期望我可以使用相同的MoneyFormatter将字符串解析回Money对象。但是,唉,没有骰子。它抛出此错误:
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对象中获得一个String对象?
编辑:@durron597,您的信息很有帮助,但没有回答问题。那么,您究竟如何从String到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()函数工作吗?
发布于 2014-05-12 14:06:28
因此,我首先注意到的是,您使用的是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)进一步的检查显示了这句话的在javadoc中。
appendCurrencySymbolLocalized
publicMoneyFormatterBuilderappendCurrencySymbolLocalized()将本地化的货币符号附加到构建器。本地化货币符号是格式化程序的区域设置所选择的符号。 符号不能被解析。 返回:这,用于链接,永远不为空。
为什么会这样?可能是因为货币符号是不明确的。许多国家使用一美元。这里有一些示例
除了世界上使用美元或比索的国家外,其他一些国家使用美元符号来表示本国货币,其中包括:
试试下面的代码:
MoneyFormatter mf = new MoneyFormatterBuilder()
.appendCurrencyCode()
.appendAmount(MoneyAmountStyle.LOCALIZED_GROUPING)
.toFormatter();这将产生类似USD 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);
}
}https://stackoverflow.com/questions/23609486
复制相似问题