gpt4 book ai didi

java - MoneyFormatter::parseMoney 抛出 MoneyFormatException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:06 25 4
gpt4 key购买 nike

使用这个 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)

所以,我的问题是:如何从 String 中获取 Money 对象?

编辑:@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() 函数正常工作吗??

最佳答案

所以对我来说突出的第一件事是您使用的是 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

publicMoneyFormatterBuilderappendCurrencySymbolLocalized()

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

这将打印类似 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);
}
}

关于java - MoneyFormatter::parseMoney 抛出 MoneyFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23609486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com