NumberUtil
Origin
The digital tool is a tool-based encapsulation for mathematical operations.
Usage
Basic Arithmetic Operations
NumberUtil.add
for addition of numeric typesNumberUtil.sub
for subtraction of numeric typesNumberUtil.mul
for multiplication of numeric typesNumberUtil.div
for division of numeric types, with overloaded methods to specify the number of decimal places to retain and the rounding mode in case of inexact division.
All four operations convert double to BigDecimal before calculation, solving the problem of inaccurate calculations with float and double types. These methods are often used in commercial calculations.
Retaining Decimal Places
There are two main methods for retaining decimal places:
NumberUtil.round
mainly encapsulates the methods in BigDecimal to retain decimal places, returning a BigDecimal. This method is more flexible and can choose rounding modes such as rounding up or down.
double te1=123456.123456;
double te2=123456.128456;
Console.log(round(te1,4));//result:123456.1235
Console.log(round(te2,4));//result:123456.1285
NumberUtil.roundStr
mainly encapsulates theString.format
method, and the rounding mode adopts rounding up.
double te1=123456.123456;
double te2=123456.128456;
Console.log(roundStr(te1,2));//result:123456.12
Console.log(roundStr(te2,2));//result:123456.13
DecimalFormat
A simple encapsulation of DecimalFormat.format
, which formats double or long numbers according to a fixed format.
long c=299792458;//speed of light
String format = NumberUtil.decimalFormat(",###", c);//299,792,458
The format mainly uses the placeholder symbols # and 0 to specify the number length. 0 indicates that the digit is filled with 0 if the position is insufficient, # indicates that the digit is pulled to this position if possible.
- 0 -> take one integer digit
- 0.00 -> take one integer digit and two decimal places
- 00.000 -> take two integer digits and three decimal places
- # -> take all integer parts
- #.##% -> count as a percentage with two decimal places
- #.#####E0 -> display as scientific notation with five decimal places
- ,### -> separate every three digits with a comma, e.g., 299,792,458
- The speed of light is ,### meters per second -> embed the format in the text
For more information about the format, please refer to: Java DecimalFormat的主要功能及使用方法 (Main Functions and Usage of Java DecimalFormat)
Whether a Number
NumberUtil.isNumber
to check if it is a numberNumberUtil.isInteger
to check if it is an integerNumberUtil.isDouble
to check if it is a floating point numberNumberUtil.isPrimes
to check if it is a prime number
Random Numbers
NumberUtil.generateRandomNumber
to generate non-repeating random numbers based on the given minimum and maximum numbers and the number of random numbers to generate a specified non-repeating array.NumberUtil.generateBySet
to generate non-repeating random numbers based on the given minimum and maximum numbers and the number of random numbers to generate a specified non-repeating array.
Integer List
The NumberUtil.range
method generates an ordered integer list based on the range and step size. NumberUtil.appendRange
adds integers within the given range to an existing set.
Others
NumberUtil.factorial
for factorial calculationsNumberUtil.sqrt
for square root calculationsNumberUtil.divisor
for calculating the greatest common divisor (GCD)NumberUtil.multiple
for calculating the least common multiple (LCM)NumberUtil.getBinaryStr
for obtaining the binary string corresponding to a numberNumberUtil.binaryToInt
binary to intNumberUtil.binaryToLong
binary to longNumberUtil.compare
compare two valuesNumberUtil.toStr
convert number to string, automatically remove trailing zeros after decimal point