![](/img/trans.png)
[英]Gson is crashing with Failed to invoke protected java.text.NumberFormat() with no args error
[英]Parse Japanese currency String with java.text.NumberFormat
我在用Java解析日語貨幣字符串時遇到問題。 日元符號似乎與Java認為的符號不匹配。 這是我的代碼:
NumberFormat f = NumberFormat.getCurrencyInstance(Locale.JAPAN);
String s = "¥200";
Number n = f.parse(s);
這將引發異常:
Exception in thread "main" java.text.ParseException: Unparseable number: "¥200"
at java.text.NumberFormat.parse(NumberFormat.java:333)
似乎Java為日元使用了不同的符號(“ \\ uFFE5”)
有人可以幫忙嗎? 謝謝
NumberFormat f = NumberFormat.getCurrencyInstance(Locale.UK);
String s = "£200";
Number n = f.parse(s);
和
NumberFormat f = NumberFormat.getCurrencyInstance(Locale.US);
String s = "$200";
Number n = f.parse(s);
會產生相同的影響。 NumberFormat適用於數字,而不適用於字符串字符;即,如果允許NumberFormat僅解析整數(默認值),則需要刪除貨幣符號。 你能做的是:
NumberFormat f = NumberFormat.getCurrencyInstance(Locale.JAPAN);
String s = "¥200";
f.setParseIntegerOnly(false);
Number n = f.parse(s);
這將使日元字符通過。 用$嘗試它失敗。 但是將本地貨幣更改為美國,則允許美元,但不允許日元。
嘗試將字符“¥”(0xC2A5)更改為“ ¥”(0xEFBFA5)。
“¥”(0xC2A5)是日語語言環境中的特殊字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.