Java: How to round a float to two, four, or other specified digits after the decimal point.

Source: Internet
Author: User

Java: How to round a float to two, four, or other specified digits after the decimal point.

I thought it was easy and never cared about it. I suddenly used it today and found that the system didn't have such a function. the method used by colleagues is to convert it to string first, then several digits, and then to float type (such as: string. valueof (c ). substring (0, String. valueof (c ). indexof (". ") + 3): I felt so uncomfortable, so I found a book and I couldn't find it. I was dizzy. I found some methods on the Internet. as shown in the following figure (both of the following are two bits. If you want to take other bits, modify them yourself ):

(1): This method is convenient for me.

Float a = 123.2334f;
Float B = (float) (math. round (A * 100)/100; (here 100 is two decimal places. If you want other digits, such as four digits, here two 100 digits are changed to 10000)

(2): This method is also simple, but it must be converted to the float type:
Import java. Text. decimalformat;
String A = new decimalformat ("####,########"). Format (100.12345 );

(3): this can also be used.

Float Ft = 134.3435f;
Int scale = 2; // set the number of digits
Int roundingmode = 4; // indicates rounding. You can select other value-based methods, such as tail and so on.
Bigdecimal BD = new bigdecimal (double) ft );
BD = BD. setscale (scale, roundingmode );
FT = BD. floatvalue ();

Here, I also wrote a small algorithm. The problem is,

"Write a program in Java and use a series of 4*(1-1/3 + 1/5-1/7 + 1/9-1/11 + ....) to obtain the approximate circumference rate. and calculate the number of The number of entries in the sequence before obtaining 3.14159?"

Here I used the number of digits after the decimal point. I wrote a paragraph myself and hope you can correct it.

Package com. Zhou;
Import java. Math. bigdecimal;
Public class yuanzhou {
Static int Index = 1;

Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Double Total = 1.00;
Float F = 3.14159f;
For (INT n = 3; n <1000000; n + = 2 ){
If (n-1) % 2 = 0 & (n-1) % 4! = 0 ){
Total = total-1/(double) N;
Index ++;
}
If (n-1) % 4 = 0 ){
Total = total + 1/(double) N;
Index ++;
}

Bigdecimal BD = new bigdecimal (total * 4 );
BD = BD. setscale (5, bigdecimal. round_down );
Float Ft = BD. floatvalue ();
System. Out. println (FT );
If (FT = f ){
Break;
}
}

System. Out. Print (INDEX );
}

}

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.