English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java.math.BigDecimal
BigDecimal gibt es insgesamt4Es gibt mehrere Methoden, lassen Sie mich zunächst zwei davon betrachten:
Erster Typ: 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 a String to construct it. 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, passing the other as a parameter, and then convert the 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 precise floating-point operations, this utility class provides precise floating-point operations, including addition, subtraction, multiplication, and rounding. */ public class Arith { // Default division operation precision private static final int DEF_DIV_SCALE = 10; // This class cannot be instantiated private Arith() { } /** * Provides precise addition operations. * * @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 operations. * * @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 operations. * * @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 operations, when the division is not exact, the precision is to the decimal place after the decimal point10The digits after the decimal place 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 operations. When the division is not exact, the precision is specified by the scale parameter, and the subsequent digits are rounded. * * @param v1 * Dividend * @param v2 * Divisor * @param scale * Indicates the need for precision 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(" "Die Skala muss eine positive Ganzzahl oder Null sein"; } BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } /** * Bietet eine genaue Runde von Dezimalstellen * * @param v Die Nummer, die gerundet werden soll * @param scale Anzahl der Nachkommastellen * @return Runde das Ergebnis */ public static double round(double v, int scale) { if (scale < 0) { throw new IllegalArgumentException(" "Die Skala muss eine positive Ganzzahl oder Null sein"; } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } };
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
声明:本文内容来源于网络,版权归原作者所有。内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#oldtoolbag.com(在发送邮件时,请将#更换为@进行举报,并提供相关证据。一经查实,本站将立即删除涉嫌侵权内容。)