Results 1 to 3 of 3
  1. #1
    bz3x is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default locale and decimalFormat confusion

    I'm a bit confused about DecimalFormat. It seems that even if I specify a format string for DecimalFormat, the decimal separator in the string is ignored if I change the locale. What I want is to always format 1.0 as "1.00" regardless of whether the locale is en or fr.

    Here is some short code:

    Java Code:
    Locale L = Locale.getDefault();
    System.out.println(L.getLanguage());
    NumberFormat nf = new DecimalFormat("#,##0.00");
    System.out.println(nf.format(1.0));
    
    L = new Locale("fr");
    Locale.setDefault(L);
    System.out.println(L.getLanguage());
    nf = new DecimalFormat("#,##0.00");
    System.out.println(nf.format(1.0));
    the output is:

    Java Code:
    en
    1.00
    fr
    1,00
    What can I do to always get "1.00" even when the locale is fr?

    Thanks!

  2. #2
    bz3x is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default Solved!

    Got it! In case anyone else is curious...

    Java Code:
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setDecimalSeparator('.');
    dfs.setGroupingSeparator(',');
    DecimalFormat df = new DecimalFormat("#,##0.00", dfs);
    		
    Locale L = Locale.getDefault();
    System.out.println(L.getLanguage());
    System.out.println(df.format(1.0));
    
    L = new Locale("fr");
    Locale.setDefault(L);
    System.out.println(L.getLanguage());
    System.out.println(df.format(1.0));
    the output:

    Java Code:
    en
    1.00
    fr
    1.00
    voila!

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Nice work. Also, thanks for posting a clear, concise code example, and using CODE tags. I was about to suggest using NumberFormat instead, but your solution is better.

    -Gary-

Similar Threads

  1. Locale with SimpleDateFormat
    By swati.jyoti in forum New To Java
    Replies: 4
    Last Post: 07-02-2009, 03:34 PM
  2. Locale example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 10:04 AM
  3. DecimalFormat class
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 12-30-2007, 04:09 PM
  4. How ro change the locale of JVM
    By ravi.ks007@hotmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-26-2007, 07:17 AM
  5. Locale
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-16-2007, 03:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •