Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting any <= 64-bit number with commas

 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to trim this code down. Does anyone have an idea on how to reduce this code footprint? Is there a simple number formatting class in Java that I missed?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about java.text.NumberFormat
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:How about java.text.NumberFormat
. . . or the % tags with the , (comma) flag?
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked through those and found none that explicitly add the commas in the correct places. Do you have an example of what you are talking about?
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.printf("%,d%n", 1234567890111213);

??

Look at the link I gave you, the String format link there, and the API for the Formatter class.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks promising. Did it work?
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, no. Not exactly. I am limited to a float it seems. The compiler does not like this:
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jake benn wrote:Well, no. Not exactly. I am limited to a float it seems. The compiler does not like this:


It's the JVM that doesn't like it. The code compiles fine.

I guess you are assuming %d represents a double - it doesn't. Try reading Campbell's link again.
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not according to Formatter. Try ctrl-F ',' with the single quotes. Actually you usually get Exceptions not compiler errors for the % tags.

Sorry, I didn't realise you were using floating point. There is no problem about the , flag with the %d tag. The problem is using the %d tag with floating-point numbers. You will have to try the %f tag, preferably with numbers for width and places after the decimal point.
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice to see you again, Joanne
You were 2 minutes 6 seconds too early.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Nice to see you again, Joanne
You were 2 minutes 6 seconds too early.


Actually you were 2 minutes 7 seconds late
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh MY! My professors would be so ashamed. My assumption about %d being 'double' was not good, not good at all. Yes the code previously written works GREAT! I only write little stuff like this to keep in practice before my next class.

On a small side note: I am technically working as a programmer but, that does not mean I am fully qualified to write code that is decent or even acceptable. I thank each person who helps me on this forum to practice so I can write compilable code first, working code next and finally well written code.

I kind-of wish you had a unique section in the forum where people could put suggestions for programs. Something for new programmers covering concepts you feel have value to know. Important concepts in which you feel not enough people have sufficient skill. Like little assignments that anyone could recommend.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jake benn wrote:Oh MY! My professors would be so ashamed. My assumption about %d being 'double' was not good, not good at all. Yes the code previously written works GREAT! I only write little stuff like this to keep in practice before my next class.

On a small side note: I am technically working as a programmer but, that does not mean I am fully qualified to write code that is decent or even acceptable. I thank each person who helps me on this forum to practice so I can write compilable code first, working code next and finally well written code.

I kind-of wish you had a unique section in the forum where people could put suggestions for programs. Something for new programmers covering concepts you feel have value to know. Important concepts in which you feel not enough people have sufficient skill. Like little assignments that anyone could recommend.


That sounds suspiciously like the Cattle Drive! Take a look...I haven't done it personally (I've been known to be pretty cheap) but I have gone through some of the assignments without the nitpicking portion.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cattle Drive is precisely what I was talking about! Thanks.

For the record, Formatter is truncating my double and thus causing a loss of precision. So I guess I refer back to the original question. Is there an equivalent way in Java to add commas to numbers <= 64-bit in size? Is my original code the only way and if so, does anyone have any suggestions for making it more succinct?
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Formatter has a default number of decimal places for doubles; I think 6. So 1.234567890123456 would be truncated to 1.234568. And for reasons you are doubtless familiar with, but you can remind yourself in no 20 here, it wouldn't be represented precisely anyway.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I have my code up to a number this large: 9223372036854775807.7085774586302733229 without loss of precision. Bigger than that is BIG numbers and I just don't feel that I need any better than what I have. The code might not be pretty but it works. I am probably going about it in the MOST naive way possible but what can I say... I am naive. For giggles I will post it.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main problem with that code is that it will only work in locales where numbers are split into triplets separated by commas. Many european countries swap the use of decimal points and commas, so what you write as 12,345.765 would be written as 12.345,765 in Spain for example. And in India, the blocks are not always three digits - 125,000 for example would be written as 1,25,000.

If you use the string formatting functionality built into the API all this will be handled for you. Note that in Campbell's last post he was only saying that the default number of decimal places is 6. You can tell it to use more (or less). It's worth having a good read of the docs that Campbell posted a link to - there's some very useful stuff in there that could save you a lot of coding time.
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you trying to use longs before and after the decimal point? That will restrict you to 19 digits at most. If you start with a String, you want to use BigDecimal, which takes a String as a constructor argument. Note what Joanne says about Locales; you can pass a Locale object to the format method. As Joanne said (thank you, Joanne ), look at the API links.

But Joanne, if you had been 2 minutes 6 seconds later, you would have beaten me by 1 second

And I'm away for the weekend, so see you all on Monday.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic