public class MathUtil extends Object
Modifier and Type | Method and Description |
---|---|
static int |
div(int a,
int b)
Returns quotient according to number theory - i.e.
|
static long |
div(long a,
long b)
Returns quotient according to number theory - i.e.
|
static int |
rem(int a,
int b)
Returns remainder according to number theory - i.e.
|
static long |
rem(long a,
long b)
Returns remainder according to number theory - i.e.
|
static double |
roundDecimal(double x)
Rounds a specified double number to a decimal number with at most
14 significant digits and at most 14 digits after decimal point.
|
public static double roundDecimal(double x)
For example, suppose you have 1 dollar and 10 cents and you pay 20 cent. You should keep 90 cents. However, the following expression is false in Java:
1.1 - 0.2 == 0.9because both 1.1 and 0.2 do not have precise representations in
double
.
To make this comparison work, you have to use roundDecimal
method:
roundDecimal(1.1 - 0.2) == 0.9
As a general rule, you should use roundDecimal
after any operation
(addition, subtraction, multiplication, division) on two decimal numbers if you
know that the result is a decimal with at most 14 significant digits and at most
14 digits after decimal point.
public static int div(int a, int b)
a
- dividendb
- divisorpublic static long div(long a, long b)
a
- dividendb
- divisorpublic static int rem(int a, int b)
a
- dividendb
- divisorpublic static long rem(long a, long b)
a
- dividendb
- divisorCopyright © 2002-2018 Devexperts LLC. All Rights Reserved.