aspose file tools*
The moose likes Beginning Java and the fly likes Formatting any <= 64-bit number with commas Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Formatting any <= 64-bit number with commas" Watch "Formatting any <= 64-bit number with commas" New topic
Author

Formatting any <= 64-bit number with commas

Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
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

Joined: Oct 25, 2008
Posts: 2700

How about java.text.NumberFormat


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Wouter Oet wrote:How about java.text.NumberFormat
. . . or the % tags with the , (comma) flag?
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

Joined: Jun 05, 2009
Posts: 82
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
That looks promising. Did it work?
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Well, no. Not exactly. I am limited to a float it seems. The compiler does not like this:
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
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.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
Nice to see you again, Joanne
You were 2 minutes 6 seconds too early.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
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

Joined: Jun 05, 2009
Posts: 82
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

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

Joined: Jun 05, 2009
Posts: 82
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

Joined: Aug 05, 2005
Posts: 3739
    
  16
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Formatting any <= 64-bit number with commas