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 String |
formatDouble(double value,
int scale)
Returns a string representation of the double argument whose scale is the specified value.
|
static void |
formatDouble(StringBuilder sb,
double value,
int scale)
Returns a string representation of the double argument whose scale is the specified value.
|
static String |
formatDoublePrecision(double value,
int precision)
Returns a string representation of the double argument with specified precision.
|
static void |
formatDoublePrecision(StringBuilder sb,
double value,
int precision)
Returns a string representation of the double argument with specified precision.
|
static double |
parseDouble(CharSequence text)
Fast implementation of double parser.
|
static double |
parseDouble(CharSequence text,
int start,
int end)
Fast implementation of double parser.
|
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.
|
static double |
roundDecimal(double value,
int scale,
RoundingMode mode)
Returns a double value scaled according to the RoundingMode and scale value.
|
static double |
roundPrecision(double value,
int precision,
RoundingMode mode)
Returns a double value rounded according to the RoundingMode and round precision.
|
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 double roundDecimal(double value, int scale, RoundingMode mode)
For Rounding Mode (UP, DOWN, CEILING, FLOOR) and values larger than 1E16 by absolute value it is possible to have different result than BigDecimal in very rare case. The positive scale parameter defines a number of fraction digits in the double value. The negative scale parameter defines a number of integer digits that will be assigned to 0.
value
- double for roundscale
- the number of fractional digits for the positive scale
and the number of integer digits to be assigned the value 0 for negative scalemode
- RoundingModeArithmeticException
- if the rounding mode is UNNECESSARY
and the operation would require rounding.public static double parseDouble(CharSequence text) throws NumberFormatException
text
- with double valueNullPointerException
- if the text
is nullNumberFormatException
- if the text
does not contain valid double valuepublic static double parseDouble(CharSequence text, int start, int end) throws NumberFormatException
text
- with double valuestart
- start position of valueend
- end position of value, exclusiveNullPointerException
- if the text
is nullNumberFormatException
- if the text
does not contain valid double valuepublic static String formatDoublePrecision(double value, int precision)
Double.toString(double)
but conforms to the following rules:
'.0'
is omitted
(Example: '1E10'
instead of '1.0E10'
for Double.toString(double))
Double.toString(double)
For a number with absolute value greater than 1E16, in very rare cases it's possible to have rounding not
exactly the same as
BigDecimal.valueOf(value).round(new MathContext(precision, RoundingMode.HALF_UP))
,
but anyway the value will match the result with RoundingMode.UP or RoundingMode.DOWN
value
- doubleprecision
- number of significant digits, effective rounding for [1,16]IllegalArgumentException
- if precision less than 0public static void formatDoublePrecision(StringBuilder sb, double value, int precision)
Double.toString(double)
but conforms to the following rules:
'.0'
is omitted
(Example: '1E10'
instead of '1.0E10'
for Double.toString(double))
Double.toString(double)
For a number with absolute value greater than 1E16, in very rare cases it's possible to have rounding not
exactly the same as
BigDecimal.valueOf(value).round(new MathContext(precision, RoundingMode.HALF_UP))
,
but anyway the value will match the result with RoundingMode.UP or RoundingMode.DOWN
sb
- StringBuilder with formatted resultvalue
- doubleprecision
- number of significant digits, effective rounding for [1,16]NullPointerException
- if the sb
is nullIllegalArgumentException
- for precision less than 0public static String formatDouble(double value, int scale)
Double.toString(double)
but conforms to the following rules:
'.0'
is omitted
(Example: '1E10'
instead of '1.0E10'
for Double.toString(double))
BigDecimal.valueOf(value).setScale(scale, RoundingMode.HALF_UP))
,
but anyway the value will match the result with RoundingMode.UP or RoundingMode.DOWN
value
- doublescale
- the number of fractional digits for the positive scale
and the number of integer digits to be assigned the value 0 for negative scalepublic static void formatDouble(StringBuilder sb, double value, int scale)
Double.toString(double)
but conforms to the following rules:
'.0'
is omitted
(Example: '1E10'
instead of '1.0E10'
for Double.toString(double))
BigDecimal.valueOf(value).setScale(scale, RoundingMode.HALF_UP))
,
but anyway the value will match the result with RoundingMode.UP or RoundingMode.DOWN
sb
- StringBuilder with formatted result with specified scalevalue
- doublescale
- the number of fractional digits for the positive scale
and the number of integer digits to be assigned the value 0 for negative scalepublic static double roundPrecision(double value, int precision, RoundingMode mode)
For Rounding Mode (UP, DOWN, CEILING, FLOOR) and values larger than 1E16 by absolute value it is possible to have different result than BigDecimal in very rare case.
value
- double for roundprecision
- number of significant digits, effective rounding for [1,16]mode
- RoundingModeArithmeticException
- if the rounding mode is UNNECESSARY
and the operation would require rounding.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–2025 Devexperts LLC. All rights reserved.