Results 1 to 8 of 8
Thread: Numbers formatting with printf
- 11-09-2011, 10:35 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 13
- Rep Power
- 0
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
- 11-09-2011, 10:38 PM #2
Re: Numbers formatting with printf
Try the DecimalFormat class.
- 11-09-2011, 10:56 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 13
- Rep Power
- 0
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.
KLast edited by Kaizer; 11-09-2011 at 10:56 PM. Reason: typo
- 11-09-2011, 11:05 PM #4
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.
- 11-10-2011, 02:46 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 13
- Rep Power
- 0
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
- 11-10-2011, 02:56 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Numbers formatting with printf
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.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
- 11-10-2011, 03:48 AM #7
Member
- Join Date
- Oct 2011
- Posts
- 13
- Rep Power
- 0
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
- 11-10-2011, 04:38 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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
-
printf
By razzle in forum New To JavaReplies: 4Last Post: 11-12-2010, 02:13 PM -
Printf problem!!!
By pinar in forum New To JavaReplies: 3Last Post: 11-06-2010, 09:44 PM -
formatting numbers in output
By andy3 in forum New To JavaReplies: 3Last Post: 05-27-2010, 06:43 PM -
Formatting numbers or decimals (around comma)
By Joris in forum Advanced JavaReplies: 1Last Post: 04-22-2010, 05:46 PM -
printf
By Jack in forum New To JavaReplies: 2Last Post: 07-04-2007, 04:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks