Come visto in this post, è necessario impostare manualmente gli spazi come separatore.
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
ad essere onesti io sono un po 'confuso, ma credo che il problemaè, che è necessario stringhe formattati (a quanto pare non tutti gli spazi bianchi è lo stesso qui)
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);
}
prega correggimi se sbaglio qui o il mio esempio non ha funzionato. Non ho idea del perché faccia cose del genere, ma per evitare situazioni confuse come quelle sopra, potresti voler impostare manualmente il separatore.
EDIT
come Joop Eggen dichiarato nel suo answer stringa deve utilizzare uno spazio unificatore disco ('\u00a0'
) invece di un semplice spazio bianco. Questo è il motivo per cui il mio esempio restituisce "123" nell'ultima riga (ho usato solo il normale spazio bianco).
Quindi un bug nel locale svedese in Java? –
Per me questa domanda e risposta di seguito è meglio di una contrassegnata come correlata. Non penso che questa domanda debba essere chiusa. – Jayan