Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Converting a number into string separated by commas.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to convert a number to string seperated by commas?
I know to convert 1234567 to 1,234,567 using this
String number = NumberFormat.getNumberInstance(Locale.CANADA).format(1234567);

but how to convert into 12,34,567.
Thanks for any help.
 
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Is that a standard format for anywhere? If so, what happens when you try this?
System.out.println(String.format(Locale.RURITANIA, "%,d", 1234567890));
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell, this is a common way to format numbers in India and unfortunately Java's NumberFormat and DecimalFormat don't seem to support this (at least not in any obvious way). I don't know the answer to the question.
 
shashank shashi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell, I tried your suggestion but eclipse is showing an error RURITANIA cannot be resolved or is not a field.
 
Campbell Ritchie
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damn!

I had tried it with Locale.INDIA but that didn't work.
Does that mean
new DecimalFormat("##,##,###")
or some other such java1.3 style horror? I suspect even that won't work, because it says that variable separations of thousands separators are removed automatically.
 
Campbell Ritchie
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruritania only exists in books by Hope. It is like American lawyers referring to a hypothetical person as John Doe.
 
Campbell Ritchie
Marshal
Posts: 74374
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Jesper is right, it is not supported by Java out of the box.

Shashank, you have to format it programmatically and it is quite doable a task.
 
shashank shashi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou all for your help.I will definitely give it a try.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a solution offered on StackOverflow that uses com.ibm.icu.text.DecimalFormat.
 
shashank shashi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:



Campbell you made my day,its perfect.
By the way,I just came into the coderanch and this was a great welcome,you guys are awesome:-)
 
Campbell Ritchie
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:There's a solution offered on StackOverflow that uses com.ibm.icu.text.DecimalFormat.

That is a nice solution. There are other solutions on that same SO thread which look pretty awful to me.

SS: What did you come up with? [Edit]Challenge: work out how to use a StringBuiider and its insert method to produce the result you require[/edit]
 
shashank shashi
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell tried using StringBuilder it works fine.Thankyou
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are on right track, just need to check few things.
Have you tried to run your code when
 
Campbell Ritchie
Marshal
Posts: 74374
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also it would be more elegant (I think) to use a while loop rather than a for loop.
 
shashank shashi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou Tapas I had missed it.Hope this is correct.
Campbell,I used while loop and measured the time of execution it was a lot fater than for.Once again,Thankyou.
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank shashi wrote:Campbell,I used while loop and measured the time of execution it was a lot fater than for.Once again,Thankyou.[code=java]


Don't know how you measured, but there should not be any difference in execution time in your case.
If I am correct, Campbell meant while loop will be more readable.
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank shashi wrote:Thankyou Tapas I had missed it.Hope this is correct.


I am afraid still there is something you need to check.
Have you tried executing your code with
 
shashank shashi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou tapas,i had missed it agian :-(.I used the below code to measure time and most of the time while loop was faster(i dont know why).There was only some nanoseconds difference.While loop is definitely more readable than for loop.

 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I executed the code several times. Few times while loop is taking more time than for loop.
So it cannot be concluded that while loop is faster than for loop, at least in this case.

And for your information, you need not include the import statement in this code.
All classes under java.lang are automatically imported in Java.
 
Marshal
Posts: 8124
572
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tapas Chand wrote:I am afraid still there is something you need to check.
Have you tried executing your code with?


Tapas, well deserved cow - for thinking about the corner cases twice in this thread, and for putted effort in assisting on multiple other threads.
 
shashank shashi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, i saw the discussion on stackoverflow regarding performance between for and while loop. Thanks for pointing out the import statement.
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Tapas, well deserved cow - for thinking about the corner cases twice in this thread, and for putted effort in assisting on multiple other threads.


Thank you so much
 
Campbell Ritchie
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank shashi wrote: . . . measured the time of execution it was a lot fater than for.Once again,Thankyou.
. . .

You're welcome

But how did you time the execution?
 
Campbell Ritchie
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see my question has already been answered, and Tapas Chand has tried it out.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic