JAVA float Double data type reserved 2-bit decimal point 5 ways

Source: Internet
Author: User
Tags float double



/**
* Java two integers divide two decimal places and convert decimals to percentages
* In Java, when two integers are divided, because the number after the decimal point is truncated, the result of the operation will be an integer, if you want to get the result of the operation as a floating point, you must convert both integers one or both to a floating point.
For example:
(float) A/b///convert one of the integers to a floating-point number and divide it with another integer
A/(float) b
(float) A/(float) b//convert two integers to floating-point numbers at the same time and then divide
*/


//////// Calculate the number of decimal places:
        System.out.println ("1/8 =" + 1 / (float) 8);
        
        float TT = Math.round (1 / (float) 8); // The return type is int.
        System.out.println ("rounded decimals:" + TT);
        System.out.println ("Rounding to decimal 2:" + Math.round (2.1546));
////method 1
        double dd = (double) (Math.round (1 / (float) 8 * 100) /100.0);
        System.out.println ("dd =" + dd);
        // (double) (Math.round (sd3 * 10000) /10000.0); This keeps 4 bits
//// Method 2
        DecimalFormat df2 = new DecimalFormat ("###. 00");
        DecimalFormat df3 = new DecimalFormat ("###. 000");

        System.out.println ("Conversion 2 =" + df2.format (1 / (float) 8));
        System.out.println ("Conversion 3 =" + df3.format (1.1256));
//// Method 3
// String ss = String.format ("% 10.2f %%", 1.1256); //1.13f
// String ss = String.format ("% 10.2f", 1.1256); // 1.13
        String ss = String.format ("% 1.2f", 1.1256); //1.13
        System.out.println ("Conversion 4 =" + ss);
//// Method 4
         double x = 23.5455;
         NumberFormat ddf1 = NumberFormat.getNumberInstance ();
         ddf1.setMaximumFractionDigits (2);
         String s = ddf1.format (x);
         System.out.print ("Number format =" + s); // rounded 23.55
//// Method 5
        float T = 0.1257f;
        BigDecimal b = new BigDecimal (T);
        T = b.setScale (2, BigDecimal.ROUND_HALF_UP) .floatValue ();
        System.out.println ("Retain 2 decimal places:" + T);

JAVA float Double data type reserved 2-bit decimal point 5 ways


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.