• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

number formatting????

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have an int (unknown lenght) that i need to format so there's a comma every 3 digits, like "1,000,203,2203". I know about DecimalFormat df1 = new DecimalFormat(), but I'm not quite sure what to put in the parentheses. If I knew the int was 4 digits, I could do "0,000", but I'm not sure how to go about formatting an unknown number of digits. Let me know!
Thanks!
 
Kate Zoy
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Turns out that even though I still don't know how to format my number, I have a bigger problem... I take in a string, which could be an integer of any length, and I parse it into an integer. Turns out that Integer.parseInt() doesn't work (in my case) for integer longer than 9 digits! I'm not sure what is going on! Help! Am i using the wrong type? Are integers not allowed 10 digits or more?
Let me know!
Thanks Alot!
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right - Integers (and ints) are stored in four bytes, and so are limited in size from -(2^31) to 2^31-1. I can't remember exactly what those numbers are but it's around 2 billion I think - so yes, 9 digits at most.

In any case, use longs for a bigger range (-(2^63) to 2^63-1), or consider the BigInteger class if you truly need no boundaries. You can use the constructor that takes a String parameter to parse into a BigInteger.

Cheers,


--Tim
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems people politely ignored my mistake. 2 billion (2 000 000 000) is of course 10 digits, not 9.

In any case, the actual range for ints is [-2,147,483,648 to 2,147,483,647]. For longs it's [-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807].



--Tim
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

Just a light hearted technical comment, on a multimeter that would be called "nine and a half digits"!

Ed
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Should get 538,927,393
 
Kate Zoy
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that helps alot! I'll go try the BigInteger right now!
Thanks again,
Kate
reply
    Bookmark Topic Watch Topic
  • New Topic