How to round floating point number in Java

--------------------------------------------

Rounding real in full:

Just use the method Math.floor () to round the real to the nearest integer:

Double a = Math. floor ( 1 . 99 ); // 1.0

Unlike the method Math.ceil () allows a real round to the nearest integer:

Double a = Math. ceil ( 1 . 01 ); // 2.0

If one wishes to respect the standard rounding rule, using the nearest higher or lower depending on the value around the decimal part, simply add 0.5 to the passed value Math.floor () :

Double a = Math. floor ( 1 . 99 + 0 . 5 ); // 2.0

Double a = Math. floor ( 1 . 49 + 0 . 5 ); // 1.0

If the value of the fraction is less than [b] 0.5 [/ b], the value will be rounded to the nearest whole number.

If the value of the fraction is greater than or equal to [b] 0.5 [/ b], the value will be rounded to the next whole number.

Warning: could also use a cast to type int or long to "lose" the fractional part, but it can cause problems with large values because the cast can lead to a loss of information by truncation. Round to a real 'n' decimals: You can also make a rounded 'n' decimals using the following code:

Select

Double a = 1 . 6,666,666 ;

// Round the value was 10 ^ -2

a * = 100 . 0 ;

a = . Math floor (a + 0 . 5 );

a / = 100 . 0 ; // 1.67

For simplicity, this method can be used directly

public static double floor ( double a, int n) {

double p = Math. pow ( 10 . 0 , n)

return . Math floor ((a * p) + 0 . 5 ) / p;

}

Which is used as follows:

Double a = floor ( 1 . 6,666,666 , 2 ); // 1.67

But be careful, however, one can obtain an incorrect result when approaching the maximum limit doubles (1e308 anyway). For more accurate results, it may be towards the class BigDecimal and methods setScale () . Rounding to display: In contrast, if the result is to convert to a string, it is best to turn to the class java.text.DecimalFormat

--------------------------------------------

Rounding real in full:

Just use the method Math.floor () to round the real to the nearest integer:

Double a = Math. floor ( 1 . 99 ); // 1.0

Unlike the method Math.ceil () allows a real round to the nearest integer:

Double a = Math. ceil ( 1 . 01 ); // 2.0

If one wishes to respect the standard rounding rule, using the nearest higher or lower depending on the value around the decimal part, simply add 0.5 to the passed value Math.floor () :

Double a = Math. floor ( 1 . 99 + 0 . 5 ); // 2.0

Double a = Math. floor ( 1 . 49 + 0 . 5 ); // 1.0

If the value of the fraction is less than [b] 0.5 [/ b], the value will be rounded to the nearest whole number.

If the value of the fraction is greater than or equal to [b] 0.5 [/ b], the value will be rounded to the next whole number.

Warning: could also use a cast to type int or long to "lose" the fractional part, but it can cause problems with large values because the cast can lead to a loss of information by truncation. Round to a real 'n' decimals: You can also make a rounded 'n' decimals using the following code:

Select

Double a = 1 . 6,666,666 ;

// Round the value was 10 ^ -2

a * = 100 . 0 ;

a = . Math floor (a + 0 . 5 );

a / = 100 . 0 ; // 1.67

For simplicity, this method can be used directly

public static double floor ( double a, int n) {

double p = Math. pow ( 10 . 0 , n)

return . Math floor ((a * p) + 0 . 5 ) / p;

}

Which is used as follows:

Double a = floor ( 1 . 6,666,666 , 2 ); // 1.67

But be careful, however, one can obtain an incorrect result when approaching the maximum limit doubles (1e308 anyway). For more accurate results, it may be towards the class BigDecimal and methods setScale () . Rounding to display: In contrast, if the result is to convert to a string, it is best to turn to the class java.text.DecimalFormat