Java preserves two decimal places for multi-method Java with a double reserved two decimal places

Source: Internet
Author: User

This article mainly introduces the Java method to keep the double type two decimal places, please refer to the use of itCopy CodeThe code is as follows:
Mport Java.text.DecimalFormat;

DecimalFormat df = new DecimalFormat ("##### #0.00");

Double D1 = 3.23456
Double d2 = 0.0;
Double d3 = 2.0;
Df.format (D1);
Df.format (D2);
Df.format (D3);

The 3 results were:

Copy CodeThe code is as follows:
3.23
0.00
2.00

Java retains two-bit decimal questions:

Way One:

Rounded

Copy CodeThe code is as follows:
Double F = 111231.5585;
BigDecimal B = new BigDecimal (f);
Double f1 = B.setscale (2, bigdecimal.round_half_up). Doublevalue ();

Keep Two decimal places

Way two:

Copy CodeThe code is as follows:
Java.text.DecimalFormat DF =new Java.text.DecimalFormat ("#.00");
Df.format (the number you want to format);

Cases:

Copy CodeThe code is as follows:
New Java.text.DecimalFormat ("#.00"). Format (3.1415926)

#.00 represents two decimal places #.0000 four decimal places and so on ...

Way three:

Copy CodeThe code is as follows:
Double d = 3.1415926;
string result = string. Format ("%.2f");

%.2f%. Indicates that any number of digits before the decimal point 2 indicates that the result of the two-bit decimal format is F for floating-point

Mode four:

Copy CodeThe code is as follows:
NumberFormat ddf1=numberformat.getnumberinstance ();
void setmaximumfractiondigits (int digits)

Digits number of digits displayed
Sets the maximum number of digits to display for the formatted object after the decimal point, the last bit shown is rounded

Copy CodeThe code is as follows:
Import java.text.*;
Import java.math.*;
Class TT
{
public static void Main (String args[])
{Double x=23.5455;
NumberFormat ddf1=numberformat.getnumberinstance ();


Ddf1.setmaximumfractiondigits (2);
String s= Ddf1.format (x);
System.out.print (s);
}
}

Copy CodeThe code is as follows:
Import java.text.*;

DecimalFormat df=new DecimalFormat (". # #");
Double d=1252.2563;
String St=df.format (d);
System.out.println (ST);

Java preserves two decimal places for multi-method Java with a double reserved two decimal places

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.