In the download from: http://blog.csdn.net/chaozhidan/article/details/8161862
How the double type data retains two decimal places.
You prawns, the existing double type of data, how to convert to a number of two-bit decimals, the return value of the type is still a double type, rather than a string type.
For example 0, return "0.00";
Hint: decimalformat df = new DecimalFormat ("#0.00");
System.out.println (Df.format (d));
This code can set the data setting of type double to hold two bits, but returns a string type, and what to do if you want to return the double type.
------Solution--------------------------------------------------------
First you have to figure out what is the model and what is the presentation.
Since "The customer's demand is to be able to 0.0 also show 0.00", then the customer did not ask to say "Hope 0 in the internal preservation time also saved to 0.00", he wanted only to show. So, no matter what you are, as long as you can show the time to turn into the format he wants to do.
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 chosen, and it is absolutely impossible to simply use double.
For double, 0 and 0.00 are always the same. If you want to ask why, I can tell you that the number that is shown in this way is more in line with the concept of the number you received from childhood, otherwise it would be a bit messy.
If you are interested, you can look at BigDecimal's Javadoc carefully, as BigDecimal can represent 0 and 0.0 and 0.00, respectively, causing a very strange phenomenon, that is, under the representation of BigDecimal: 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 numbers are the most recent number that is less than or equal to the number.
Round_floor The value less than or equal to the number
Round_half_down Five homes six into
Round_half_even Rounded to the nearest side unless the two sides (distance) are equal, if so, if the reserved digits are odd, use round_half_up, if an even number, use the Round_half_down
Round_half_up Rounded
Round_unnecessary The results are accurate and do not require rounding mode
Round_up Contrary to the Round_down. |