Java's several ways to retain several decimal places and add thousands separator __java

Source: Internet
Author: User
Tags db2
Package decimal;
Import Java.math.BigDecimal;
Import Java.text.DecimalFormat;

Import Java.text.NumberFormat; /** * Java several ways to retain several decimal places and add thousands separator * <p>ClassName:TestDecimal</p> * <p>description: Keep a few decimal places </p> * <p>Author:Administrator</p> * <p>date:2017 Year April 26 </p> * * Public class Testdecimal {public
        static void Main (string[] args) {//mode a small number of less than 4 people are several, more than 4 people leave only 4-digit double DD1 = 911.911;
        Double dd2 = 911.911911;
        DecimalFormat df = new DecimalFormat ("#.0000");
        DD1 = double.parsedouble (Df.format (DD1));
        DD2 = double.parsedouble (Df.format (DD2));
        System.out.println (DD1);
        System.out.println (DD2);
        System.out.println ("////////");
        The way two decimal places less than 4 people with 0 complement, more than 4 people leave only 4 digits double ds1 = 911.911;
        Double ds2 = 911.911911;
        String RESULT1 = String.Format ("%.4f", DS1);
        String result2 = String.Format ("%.4f", DS2);
 System.out.println (RESULT1);       System.out.println (RESULT2);
        System.out.println ("////////");
        The mode of the three decimal places of less than 4 people are several, more than 4 people left only 4, and rounded double db1 = 911.911;
        Double DB2 = 911.91186;
        BigDecimal bd1 = new BigDecimal (DB1);
        BigDecimal bd2 = new BigDecimal (DB2);
        DB1 = Bd1.setscale (4, bigdecimal.round_half_up). Doublevalue ();
        DB2 = Bd2.setscale (4, bigdecimal.round_half_up). Doublevalue ();
        System.out.println (DB1);
        SYSTEM.OUT.PRINTLN (DB2);
        System.out.println ("////////");
        Mode four number of decimal places less than 4 people are several, more than 4 people left only 4 double dn1 = 911.911;
        Double dn2 = 911.911911;
        NumberFormat NF = numberformat.getnumberinstance ();
        Nf.setmaximumfractiondigits (4);
        String str1 = Nf.format (DN1);
        String str2 = Nf.format (DN2);
        System.out.println (STR1);
        System.out.println (STR2);
        System.out.println ("////////");
        Add thousand separator double n = 1000.3; DecimalFormat DF2 = new DecimalforMat ("#,###.00");
        String m = Df2.format (n);
    System.out.print (m); }
}

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.