2014-10-08 9 views
12

ho cercato di analizzare una stringa (14 123) per un lungo in Java utilizzando locale svedese utilizzando questo codice:Parse String a Long utilizzando un locale specifico (sv) e NumberFormat

String longString = "14 123" 
NumberFormat swedishNumberFormat = NumberFormat.getInstance(new Locale("sv")); 
System.out.println(swedishNumberFormat.parse(longString).longValue()); 

L'uscita di questo codice è 14 (dovrebbe essere 14123). Come da this question ho provato con sia la lingua sv che sv_SE ma questa volta il risultato è stato identico in entrambi i casi.

Secondo http://www.localeplanet.com/java/sv/index.html e http://www.localeplanet.com/java/sv-SE/index.html il separatore di raggruppamento in entrambi i casi è uno spazio () così perché la stringa lunga analisi non gestisce una, per il locale, formattato correttamente valore doppio memorizzato come stringa?

+0

Quindi un bug nel locale svedese in Java? –

+3

Per me questa domanda e risposta di seguito è meglio di una contrassegnata come correlata. Non penso che questa domanda debba essere chiusa. – Jayan

risposta

12

Lo svedese, come anche il francese, ha bisogno di un duro lavoro. spazio irrinunciabile.

longString = longString.replace(' ', '\u00a0'); 

Ingombrante.

+0

ora ho capito :) – GameDroids

+1

@GameDroids Sì, è una decisione progettuale assolutamente fuorviante utilizzare uno spazio non interruttivo come separatore di raggruppamenti. Forse volevano evitare interruzioni di parola sui numeri generati 'format'. –

0

Potrebbe provare

analizzare la stringa con impostare manualmente DecimalFormat. Costruirlo, configurare per avere setGroupingUsed (true), setDecimalSymbols con il proprio DecimalFormatSymbols.

DecimalFormat df=new DecimalFormat(); 
df.setGroupingSize(3); 
df.setGroupingUsed(true); 
DecimalFormatSymbols newSymbols=new DecimalFormatSymbols(); 
newSymbols.setGroupingSeparator(' '); 
df.setDecimalFormatSymbols(newSymbols); 
df.parse(longString) 

Seconda opzione è eseguire il debug del codice vedere l'istanza swedishNumberFormat di cui si dispone. ispezionare i campi groupingSize, decimalFormatSymbols.groupingSeparators.

0

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).

Problemi correlati