Results 1 to 8 of 8
  1. #1
    Kaizer is offline Member
    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0

    Default Numbers formatting with printf

    Hello there! I'm another Java newbie, and I was just wondering what the magic formula was to make printf spew out my numbers like so:
    1 565 434 with a space for every 10^3;10^6;10^9..etc instead of 1565434 and alternatively, how do I achieve the following:
    1,565,434.0 (<-- doesnt matter in this case for decimals, was just to show comma's versus dots. in the case Im using this all i ever get are integers so I wont even be getting the " . " notation in my results)

    thanks a lot!
    K

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: Numbers formatting with printf

    Try the DecimalFormat class.

  3. #3
    Kaizer is offline Member
    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0

    Default Re: Numbers formatting with printf

    ok, now how do you do the first notation? i got it to put comma's at every 3 step, but its not the same system for spaces apparently. its disregarding the formatting I'm 'showing' it.
    K
    Last edited by Kaizer; 11-09-2011 at 10:56 PM. Reason: typo

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: Numbers formatting with printf

    Yeah. I wasn't sure myself, tested it and got the same results. Unless someone else has a better suggestion you probably will have to do the formatting yourself. It shouldn't be that hard.

  5. #5
    Kaizer is offline Member
    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0

    Default Re: Numbers formatting with printf

    next quick question, still about same topic.
    if i want printf to align output on the right of 10 spaces, in the case of double values displayed as integers, i'd write printf("%10d",value1);
    but, how do I get to the do the same for an object? in the case of DecimalFormat?
    for it to work you need to make an object of class DecimalFormat and use that object's format method in as argument. So
    printf(DecimalFormatObj.format(value1)); works fine, but not
    printf("%10d",DecimalFormatObj.format(value1)). I know it might look dumb to be double formatting and thats why I was hoping printf had a formula to output things as 1,000.0
    Thanks a lot for the help!
    K

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Numbers formatting with printf

    I know it might look dumb to be double formatting and thats why I was hoping printf had a formula to output things as 1,000.0
    I am partial to printf() and certainly it will put in commas (or the locale default thousands separator), align how you want, and round to any desired number of decimal places. See, eg, reply #6 in this recent thread.

  7. #7
    Kaizer is offline Member
    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0

    Default Re: Numbers formatting with printf

    Awesome thanks ! xD I just like how printf facilitates 'generic' formatting when you get lots of lines of output with misc numbers )
    but, do you know how to get integers, type double for bigness^^, displayed with " " -spaces- instead of the comma's for exact same case as previous. im aiming at:
    10 000.0 for 10^4
    much obliged !
    K

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Numbers formatting with printf

    DecimalFormat is more flexible about the pattern to be used. As far as I know the only options for printf() etc are for thousands not to be separated at all, or to be separated by whatever the locale says is the thousands separator. (Eg , in the US or NZ).

    You could use String.format() then replace commas with spaces, but that's a bit yukky really.

Similar Threads

  1. printf
    By razzle in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 02:13 PM
  2. Printf problem!!!
    By pinar in forum New To Java
    Replies: 3
    Last Post: 11-06-2010, 09:44 PM
  3. formatting numbers in output
    By andy3 in forum New To Java
    Replies: 3
    Last Post: 05-27-2010, 06:43 PM
  4. Formatting numbers or decimals (around comma)
    By Joris in forum Advanced Java
    Replies: 1
    Last Post: 04-22-2010, 05:46 PM
  5. printf
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:31 AM

Posting Permissions

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