Jump to content
xisto Community
Sign in to follow this  
beeseven

Need Help With Decimalformat Showing too many/not enough decimals

Recommended Posts

Right now I have this:

DecimalFormat form = new DecimalFormat("0.00");form.toPattern();form.format(tot);
The Java API said that 0 would be a digit that you didn't want ignored (ie an extra zero), but it's only showing the first 0 after the decimal. This is a monetary value so I have to have exactly 2. Also sometimes it weirds out and goes from 0.05 to 0.0600...002 or from 0.09 to 0.0999..., so I need it to round to those 2 decimals, too.

As for the DecimalFormat I've tried "#.##", "0.#0", "#.#0", and "0.##" but those didn't work, either.

Share this post


Link to post
Share on other sites

hi beeseven,Your code seems fine. I tried it out and the results came as expected i.e, with 2 decimal places(either rounded of if more decimal places exist or 0 appended if decimal places not sufficient).you also do not need to call form.toPattern() method. but that does not make a difference anyway.what is the data type of 'tot' that you are passing to format() method. That might be affecting the output. btw fyi I tried it out on JDK 1.4.2, if the issue is still not solved, could you post more of the code.Also check out if you are using the DecimalFormat class from the java.text package only. It might be the a class with the same name existing in some other package in your classpath has been accidentally imported. This happens in case you are using IDE and you have chosen the import from a drop down or something automatically.Cheers.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.