Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

number convert

 
Jackie Wang
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any build in function can convert
a number 8000000000000
into
8, 000, 000, 000, 000
thanks!
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jackie,
you can use NumberFormatter for the purpose. here is a link for SUN's tutorial about it.
also here is a sample code i tried with it,

the output was,
Output:8,000,000,000
the only problem i found was- format() method doesn't accept long as simple argument even if we cast 8000000000 to long explicitly, it expects integer argument instead so i had to use double to specify that big value..
hope this helps.
regards
maulin
 
Jackie Wang
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maulin,
thanks so much for the prompt reply.
However, I would like the number to be
8, 000, 000, 000 (with space)
Also,
for a string: "testingmessingtestingmessageasdasdasasdasasd" how can i arbitrary break the string into 10 character with a space in between? Thanks!
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jackie,
say you want to put a space at character position 'pos' and you have the string 'str' then you can write the following to achieve that,
str = "8000000000";
char space = ' ';
str = new StringBuffer(str).insert(pos,space).toString();
please let me know if this works. ive not tried this code...
regards
maulin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic