NumberUtil
Origin
The digital tool is a tool-based encapsulation for mathematical operations.
Usage
Basic Arithmetic Operations
NumberUtil.addfor addition of numeric typesNumberUtil.subfor subtraction of numeric typesNumberUtil.mulfor multiplication of numeric typesNumberUtil.divfor 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.roundmainly 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.roundStrmainly encapsulates theString.formatmethod, 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.isNumberto check if it is a numberNumberUtil.isIntegerto check if it is an integerNumberUtil.isDoubleto check if it is a floating point numberNumberUtil.isPrimesto check if it is a prime number
Random Numbers
NumberUtil.generateRandomNumberto 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.generateBySetto 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.factorialfor factorial calculationsNumberUtil.sqrtfor square root calculationsNumberUtil.divisorfor calculating the greatest common divisor (GCD)NumberUtil.multiplefor calculating the least common multiple (LCM)NumberUtil.getBinaryStrfor obtaining the binary string corresponding to a numberNumberUtil.binaryToIntbinary to intNumberUtil.binaryToLongbinary to longNumberUtil.comparecompare two valuesNumberUtil.toStrconvert number to string, automatically remove trailing zeros after decimal point