Java: Rounding the double value to five, retaining the two decimal places, double rounding to five

Source: Internet
Author: User

Java: Rounding the double value to five, retaining the two decimal places, double rounding to five
1. Functions

The double value in the program is accurate to the two digits after the decimal point. It can be rounded up or truncated.

For example, if the input value is 12345.6789, the output value can be 12345.68 or 12345.67. You can use the RoundingMode. UP/RoundingMode. DOWN parameter to determine whether to rounding the value ).

2. Implementation Code
Package com. clzhang. sample; import java. math. bigDecimal; import java. math. roundingMode; import java. text. decimalFormat; import java. text. numberFormat; public class DoubleTest {/*** retain two decimal places, rounding an old method * @ param d * @ return */public static double formatDouble1 (double d) {return (double) Math. round (d * 100)/100 ;}
/*** The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. * @ param d * @ return */public static double formatDouble2 (double d) {// the old method is no longer recommended. // BigDecimal bg = new BigDecimal (d ). setScale (2, BigDecimal. ROUND_HALF_UP );
// New method. If no rounding is required, you can use RoundingMode. DOWN BigDecimal bg = new BigDecimal (d). setScale (2, RoundingMode. UP );
Return bg. doubleValue ();}/*** NumberFormat is the abstract base class for all number formats. * This class provides the interface for formatting and parsing numbers. * @ param d * @ return */public static String formatDouble3 (double d) {NumberFormat nf = NumberFormat. getNumberInstance ();
// Retain two decimal places nf. setMaximumFractionDigits (2 );
// If no rounding is required, you can use RoundingMode. DOWN nf. setRoundingMode (RoundingMode. UP );
Return nf. format (d );}
/*** This method is quite simple. * DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. * @ param d * @ return */public static String formatDouble4 (double d) {DecimalFormat df = new DecimalFormat ("#. 00 ");
Return df. format (d );}
/*** This method is quite convenient if it is only used to format the values in the program and then output them. * It should be used like this: System. out. println (String. format ("%. 2f ", d); * @ param d * @ return */public static String formatDouble5 (double d) {return String. format ("%. 2f ", d);} public static void main (String [] args) {double d = 12345.67890; System. out. println (formatDouble1 (d); System. out. println (formatDouble2 (d); System. out. println (formatDouble3 (d); System. out. println (formatDouble4 (d); System. out. println (formatDouble5 (d ));}}
3. Output

12345.68
12345.68
12,345.68
12345.68
12345.68

In a French Environment, except for the first two methods, the last three methods will display the decimal point as a comma.

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.