如this post所示,您需要手動將空白區設置爲分隔符。
try {
String longString = "14 123";
DecimalFormat decimalFormat = new DecimalFormat(); // instead of NumberFormat, use DecimalFormat
DecimalFormatSymbols symbols = new DecimalFormatSymbols(new Locale("sv", "SE"));
symbols.setGroupingSeparator(' '); // set the whitespace manually as grouping seperator
decimalFormat.setDecimalFormatSymbols(symbols);
System.out.println(svSE.parse(longString));
} catch (ParseException ex) {
Logger.getLogger(Playground.class.getName()).log(Level.SEVERE, null, ex);
}
//> output is 14123
說實話我有點困惑,但我認爲問題是,你需要格式化字符串(顯然不是每個空格是這裏的相同)
try {
long testNumber = 123987l;
NumberFormat swedishNumberFormat = NumberFormat.getInstance(new Locale("sv"));
//here I format the number into a String
String formatedString = swedishNumberFormat.format(testNumber);
System.out.println(formatedString); // result: "123 987"
// when parsing the formated String back into a number
System.out.println(swedishNumberFormat.parse(formatedString)); // result: "123987"
// but when parsing a non formated string like this one
System.out.println(swedishNumberFormat.parse("123 987")); // result "123"
} catch (ParseException ex) {
Logger.getLogger(Playground.class.getName()).log(Level.SEVERE, null, ex);
}
請糾正我,如果我在這裏錯了,或我的例子沒有工作。我不知道爲什麼它做這樣的事情,但爲了避免混淆像上面這樣的情況,您可能需要手動設置分隔符。
編輯
如喬普埃根在他的answer規定的字符串需要使用硬,不間斷空格('\u00a0'
),而不是一個簡單的空白。這就是爲什麼我的例子在最後一行返回「123」的原因(我只使用普通的空格)。
因此,在Java的瑞典語言環境中的錯誤? – 2014-10-08 11:08:58
對我來說,下面的這個問題和答案比標記爲相關的問題要好。我不認爲這個問題應該關閉。 – Jayan 2014-10-08 11:47:25