• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

formatting amount

 
Jas Oberai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a number like 120000,I am trying to format it like 1200.00.
I am using this function,but I am getting the result as 1,200.00.Can someone
please tell me what changes I have to make to get the desired results.Thanking you in
advance
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try just using the DecimalFormat passing your own Format String instead of relying on the Local Based Currency Format
 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want a class that I created to return a currency format i.e. (30.00 just two decimals at the end for cents) I have the print statement put the $ and dollars at the end.. The problem I am running into is using the below code suggested in this thread. My value is a double how to I convert it to a string? And is there anything in the main program I have to change since I will be passing back a string instead of a double?






Thanks,

Pete
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know the formatting answer, but I'll chime in with this: Using doubles for money will make you unhappy in the long run. The rounding and truncation will not be what you expect. A decimal class or longs that keep the value in pennies (ie $1.00 == 100) are probably better choices.
 
Alan Jump
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pete Tyo:
I want a class that I created to return a currency format i.e. (30.00 just two decimals at the end for cents) I have the print statement put the $ and dollars at the end.. The problem I am running into is using the below code suggested in this thread. My value is a double how to I convert it to a string? And is there anything in the main program I have to change since I will be passing back a string instead of a double?


Instead of using DecimalFormat(), you could use String.format(), something like this:

It's a bit of a shortcut, but it would leave myValue untouched for further use, and still produce the desired output format.
 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
appreciate all the suggestions... Finally used the below code to get my desired output from a double...



Thanks,

Pete
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic