# Rounding functions in Java Math.Round () Java _ block chain

Source: Internet
Author: User

public class Mathtest {
public static void Main (string[] args) {

System.out.println ("The first digit after the decimal point =5");
SYSTEM.OUT.PRINTLN ("Positive number: Math.Round (11.5) =" + Math.Round (11.5));
SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.5) =" + math.round (-11.5));
System.out.println ();

System.out.println ("The first digit after the decimal point <5");
SYSTEM.OUT.PRINTLN ("Positive number: Math.Round (11.46) =" + Math.Round (11.46));
SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.46) =" + Math.Round (-11.46));
System.out.println ();

System.out.println ("The first digit after the decimal point >5");
SYSTEM.OUT.PRINTLN ("Positive number: Math.Round (11.68) =" + Math.Round (11.68));
SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.68) =" + Math.Round (-11.68));
}
}

Run Result:

1, the first place after the decimal point =5
2. Positive number: Math.Round (11.5) =12
3, Negative: Math.Round (-11.5) =-11
4,
5, the first place after the decimal point <5
6. Positive number: Math.Round (11.46) =11
7, Negative: Math.Round (-11.46) =-11
8,
9, the first place after the decimal point >5
10. Positive number: Math.Round (11.68) =12
11, Negative: Math.Round (-11.68) =-12

Based on the results of the above example, we can also summarize in the following way, perhaps more easily memory:

1, the first digit after the decimal point of the parameter, the result of the <5 is the integer part of the parameter.
2, the first digit after the decimal point >5, the result is the parameter integer part absolute value +1, the symbol (i.e. positive or negative) unchanged.
3. After the decimal point of the parameter is the first digit = 5, the positive result is an integer part +1, and the negative operation result is an integral part.

End: Greater than five all plus, equal to five positive plus, less than five total.

Math.Round

Grammar:
Math.Round (x);
Parameters:
X is a numeric value.
Explain:
Method. Returns the integer approximation of the parameter x rounded.

round

`round (Double A)`
Returns a long that is closest to the argument. The result is rounded to an integer: Plus 1/2, the result is called floor and the resulting result is cast to a long type. In other words, the result equals the value of the following expression:

`(long) Math.floor (A + 0.5d)`

The special case is as follows: If the argument is NaN, then the result is 0. If the result is a negative infinity or any value less than or equal to long.min_value, the result is equal to the Long.min_value value. If the argument is positive infinity or any value greater than or equal to long.max_value, the result is equal to the Long.max_value value.

parameter: A-floating point value rounded to long. returns: The parameter value rounded to the nearest long value. Round

`round (Float a)`
Returns the int with the nearest parameter. The result is rounded to an integer: Plus 1/2, the result is called floor and the resulting result is cast to the int type. In other words, the result equals the value of the following expression:

`(int) Math.floor (A + 0.5f)`

The special case is as follows: If the argument is NaN, then the result is 0. If the result is a negative infinity or any value less than or equal to integer.min_value, the result is equal to the Integer.min_value value. If the argument is positive infinity or any value greater than or equal to integer.max_value, the result is equal to the Integer.max_value value.

parameter: A-floating-point value to be rounded to an integer. returns: The parameter value rounded to the nearest int value. ---------------------

4.3<4.4<4.5 so
Math.Round (4.3) =4
Math.Round (4.4) =4
Math.Round (4.5) =5

-4.6<-4.5 <-4.4 So
Math.Round (-4.6) =-5
Math.Round (-4.5) =-4
Math.Round (-4.4) =-4

-4.51 |-4.50-4.49

4.49 | 4.50 4.51

Because it is negative, so the critical point is on the left side of 5, the text of the "rounding", it is easy to confuse people

Rounding is calculated to the right:
-----( -5)-----( -4.6) ( -4.5) ( -4.4)-----( -4)----------(0)----------(4)-----(4.4) (4.5) (4.6)-----(5)-----
-----( -5) <---( -4.6) ( -4.5)----------> ( -4)----------(0)----------(4) <----------(4.5) (4.6)---> (5)---- -
-------------------------------( -4.4)---> ( -4)---------(0)-----------(4) <---(4.4)------------------------ ----
Note that the position of these digits, positive and negative numbers are not symmetric relations, Math.Round () is the operation is left to right, so:
4.5 Rounding should be a large value for 5,-4.5 Also, take a large value of-4, because -4>-4.5>-5

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

The math class provides three methods related to rounding: Ceil,floor,round, which correspond to the meaning of their English names, for example: Ceil's meaning in English is the ceiling, which means that it is rounded up, Math.ceil (11.3) The result of 12,math.ceil (-11.6) is the result of -11;floor is the floor, the method means downward rounding, Math.floor (11.6) The result is 11,math.floor (-11.4) Results-12; The most difficult to master is the round method, he said "rounding", the algorithm is Math.floor (x+0.5), the original number plus 0.5 and then down the whole, so, math.round (11.5) The result is 12,math.round (- 11.5) The result is-11. Math.Round () In line with this rule: after the decimal point is greater than 5 all add, equal to 5 positive plus, less than 5 total. Let's take a look at the JDK instructions:
(1) public static long round (double a)
Returns the closest long to the argument. The result was rounded to a integer by adding 1/2, taking the floor of the "result," and casting the result to type long. In the ' other words ', the result is equal to the value of the expression:

(long) Math.floor (A + 0.5d)

(2) public static double floor (double a)
Returns the largest (closest to positive infinity) Double value This is not greater than the argument and are equal to a mat Hematical integer.special Cases:
If the argument value is already equal to a mathematical integer, then the ' result ' is the same as the argument.
If the argument is Nan or a infinity or positive zero or negative zero, then the result is the same as the argument.

Parameters
A-a value.
Returns
The smallest (closest to negative infinity) floating-point value this is not less than the argument and are equal to a math Ematical Integer.

Related Keywords:

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.