The double type in Java displays two decimals, such as 12.00

Source: Internet
Author: User

How does a double type of data retain two decimal places? Prawns, you. The existing double type of data, how to convert to a number that retains two decimal places, the type of the return value is still a double type, not a string type. For example 0, return "0.00";
Tip: DecimalFormat df = new DecimalFormat ("# # #0.00");     System.out.println (Df.format (d)); This code can be a double type of data set to retain two bits, but the return is a string type, want to return a double type, what to do?
------Solution--------------------------------------------------------
First you have to figure out what the model is and what is unfolding.
Since "customer demand is to be able to display 0.0 also as 0.00", then the customer did not ask that "hope 0 in the internal preservation of the time also saved as 0.00", he wanted to just show. So, no matter what you save, as long as you can show the time to turn into the format he wants.
If you want to store a 0 in the form of 0.00, then I have to tell you that only string and BigDecimal can be selected, it is absolutely impossible to simply use a double implementation.
For double, 0 and 0.00 are always the same. If you want to ask why, I can tell you, so that the number of means you will feel more in line with the concept of the number you have received, otherwise it will be a bit messy.
If you are interested, you can carefully first look at BigDecimal's Javadoc, because BigDecimal can respectively represent 0 and 0.0 and 0.00 and so on, resulting in a very strange phenomenon, that is, in the bigdecimal of the expression: 0.equals (0.0) and 0. Equals (0.00) will return false, while 0.compareTo (0.0) and 0.compareTo (0.00) will return 0. BigDecimal ratedecimal = new BigDecimal (12);//12 is a double type

BigDecimal Num=ratedecimal.setscale (1, bigdecimal.round_half_up);

Num is 12.00; use BigDecimal to help show the back 0

Round_ceiling The nearest value that is greater than or equal to the number
Round_down positive number is less than or equal to the number of the nearest, negative number is greater than or equal to the number of the nearest
Round_floor the value that is less than or equal to the number
Round_half_down five-loft six-in
Round_half_even rounds to the nearest side (distance), unless both sides (distance) are equal, if so, if the reserved bits are odd, use round_half_up, if an even number, use Round_half_down
Round_half_up rounding
Round_unnecessary calculation results are accurate and do not require rounding mode
Round_up and Round_down opposite

The double type in Java displays two decimals, such as 12.00

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.