Formatting numbers with spaces between them

Discussion in 'Java' started by hulkko123, Mar 20, 2006.

  1. hulkko123

    hulkko123 Guest

    Hello,

    I have String containing only numbers and a comma in followin format
    nnnnnnnnn,n where n is number 0 - 9.
    E.g. the right value would be 121231234,1

    I have to convert it into format 12 123 1234,1

    How should I code it properly using Java's Formatter classes?
    I can code that using loops etc. but I do not want to code such
    code. So, anyone know how to do it with formatters. I have to support
    JDK 1.4.

    I tried a code below, but spaces in the last line does not appear
    between
    numbers. I know that might approach can also be wrong.
    Could someone please, how handle this formatting nicely.

    String s = "121231234,1";
    double d = Double.parseDouble(s);

    DecimalFormat df = new DecimalFormat("## ### ####.0");

    System.out.println(String.format("## ### ####.0", d));


    // Output is still 121231234,1 but I would like have
    // it in form 12 123 1234,1


    Cheers!
     
    hulkko123, Mar 20, 2006
    #1
    1. Advertisements

  2. hulkko123

    Rhino Guest

    I assume that you want this formatting because you are trying to please a
    user who is used to seeing numbers in that format.

    Different countries and regions have different expectations on the way
    numbers should look so I would solve your problem by taking advantage of the
    Java support for Internationalization (often abbreviated 'i18n') and
    Localization (often abbreviated 'l10n'). Java offers substantial support for
    i18n/l10n as described in the Java Tutorial, beginning here:
    http://java.sun.com/docs/books/tutorial/i18n/index.html

    To illustrate how simple this can make your code, I wrote this short
    fragment that solves your problem:

    int myInt = 123456789;

    double myDouble = 123456789.123;

    NumberFormat numberFmt = NumberFormat.getNumberInstance(new Locale("fr",
    "CA"));

    String formattedInt = numberFmt.format(myInt);

    String formattedDouble = numberFmt.format(myDouble);

    System.out.println("myInt = " + myInt);

    System.out.println("myDouble = " + myDouble);

    System.out.println("formattedInt = " + formattedInt);

    System.out.println("formattedDouble = " + formattedDouble);

    In the code, an int, named 'myInt', and a double, named 'myDouble', are
    created and initialized. Then, a Java NumberFormat, named 'numberFmt', is
    created assuming that users are in the Locale associated with the
    French-speaking parts of Canada. Then, for each number that I want to
    display (write to a file, show on a screen, etc. but not use in a
    calculation), I apply 'numberFmt' to that number, creating a String
    representation of the number. Lastly, the numbers are displayed and you can
    see that the formatted int uses blanks as the thousands separator. The
    formatted double also uses blanks as the thousands separator and uses a
    comma as the decimal point.

    All you need to do is use the appropriate values for the Locale you are in
    and your numbers will be displayed in the format normally used in your area.

    This isn't the only solution to your problem but I think it is probably
    easier than the approach you were using.
     
    Rhino, Mar 20, 2006
    #2
    1. Advertisements

  3. hulkko123

    opalpa Guest

    DecimalFormat uses one width for splitting numbers into segments.

    Separately: does your local use "," instead of "." for doubles? That
    is your String s is going to thrown an exception in my environment.
    Also "System.out.println(String.format("## ### ####.0", d));" does not
    use format you are trying to create.

    Opalinski

    http://www.geocities.com/opalpaweb/
     
    opalpa, Mar 20, 2006
    #3
  4. hulkko123

    Bobby E Guest

    Yes, in many European country decimal is , not .
    Is it possible to change decimal character from . to , and use Formatter?

    Any how thanks for your reply.
     
    Bobby E, Mar 20, 2006
    #4
  5. hulkko123

    opalpa Guest

    Is it possible to change decimal character from . to , and use Formatter?

    Yes, look at Rhino's post. If you want to directly change formater's
    symbols look into using setDecimalFormatSymbols .

    Although I knew different places used different characters for decimal
    point in output and input I did not know that Java source code could be
    written using them also.

    Opalinski

    http://www.geocities.com/opalpaweb/
     
    opalpa, Mar 20, 2006
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.