Using format()/printf() for formatting and Scanner for parsing localized elements

format() from String or printf() from e.g. PrintWriter will use the default language except you give a Local object as the first argument:

String s1 = String.format( Locale.ENGLISH, "%,.2f", 1000234234.56772345 );
String s2 = String.format( Locale.FRANCE, "%,.2f", 1000234234.56772345 );
System.out.println( s1 ); // 1,000,234,234.57
System.out.println( s2 ); // 1 000 234 234,57

And the class Scanner offers nextDouble() that uses the default language too except you use useLocale().

double s1p = new Scanner( s1 ).useLocale( Locale.ENGLISH ).nextDouble();
double s2p = new Scanner( s2 ).useLocale( Locale.FRENCH ).nextDouble();

The methods format()/printf() and the class Scanner are very handy and easy to use.

Ähnliche Beiträge

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert