• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

formatting amount

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try just using the DecimalFormat passing your own Format String instead of relying on the Local Based Currency Format
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
appreciate all the suggestions... Finally used the below code to get my desired output from a double...



Thanks,

Pete
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic