English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java.math.BigDecimal
BigDecimal has a total of4There are enough methods, let me take a look at two of them first:
First type: BigDecimal(double val)
Translates a double into a BigDecimal.
Second: BigDecimal(String val)
Translates the String representation of a BigDecimal into a BigDecimal.
To use BigDecimal, you need to use String to construct. To perform an addition operation, you need to first convert two floating-point numbers to String, then construct BigDecimal, call the add method on one of them, pass the other as a parameter, and then convert the operation result (BigDecimal) back to a floating-point number.
public static double add(double v1,double v2) public static double sub(double v1,double v2) public static double mul(double v1,double v2) public static double div(double v1,double v2) public static double div(double v1,double v2,int scale) public static double round(double v, int scale)
Utility class: Arith
/** * Since Java's simple types cannot perform floating-point operations accurately, this utility class provides precise floating-point arithmetic, including addition, subtraction, multiplication, division, and rounding. */ public class Arith { // Default division operation accuracy private static final int DEF_DIV_SCALE = 10; // This class cannot be instantiated private Arith() { } /** * Provides precise addition operation. * * @param v1 * Minuend * @param v2 * Addend * @return sum of two parameters */ public static double add(double v1, double v2) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.add(b2).doubleValue(); } /** * Provides precise subtraction operation. * * @param v1 * Minuend * @param v2 * Subtrahend * @return difference of two parameters */ public static double sub(double v1, double v2) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.subtract(b2).doubleValue(); } /** * Provides precise multiplication operation. * * @param v1 * Dividend * @param v2 * Multiplier * @return product of two parameters */ public static double mul(double v1, double v2) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.multiply(b2).doubleValue(); } /** * Provides (relative) precise division operation, when a division that cannot be divided evenly occurs, it is accurate to the decimal place after the decimal point10digit, and the subsequent digits are rounded. * * @param v1 * Dividend * @param v2 * Divisor * @return quotient of two parameters */ public static double div(double v1, double v2) { return div(v1, v2, DEF_DIV_SCALE); } /** * Provides (relative) precise division operation. When a division that cannot be divided evenly occurs, the precision specified by the scale parameter is used, and the subsequent digits are rounded. * * @param v1 * Dividend * @param v2 * Divisor * @param scale * Indicates the need for accuracy to the decimal place after the decimal point. * @return quotient of two parameters */ public static double div(double v1, double v2, int scale) { if (scale < 0) { throw new IllegalArgumentException( "The scale must be a positive integer or zero" } BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } /** * Provide precise decimal place rounding processing. * * @param v The number to be rounded * @param scale The number of decimal places to retain * @return The rounded result */ public static double round(double v, int scale) { if (scale < 0) { throw new IllegalArgumentException( "The scale must be a positive integer or zero" } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } };
That's all for this article. I hope it will be helpful to everyone's learning and also hope everyone will support the Yelling Tutorial.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace '#' with '@' when sending an email for reporting. Provide relevant evidence, and once verified, this site will immediately delete the infringing content.