English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java.math.BigDecimal
BigDecimal comporte un total de4Il y a suffisamment de méthodes, laisssez-moi d'abord voir deux de leurs utilisations :
Première méthode : BigDecimal(double val)
Translates a double into a BigDecimal.
Second method: 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 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 precisely, this utility class provides precise floating-point operations, including addition, subtraction, multiplication, division, 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 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 * multiplicand * @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, and when the division is not exact, it is rounded to the decimal places after the decimal point10place, and the following 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 the division is not exact, the precision is specified by the scale parameter, and the following digits are rounded. * * @param v1 * dividend * @param v2 * divisor * @param scale * indicating the precision to the decimal places 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(); } /** * Fournit un traitement d'arrondi de décimales précis. * * @param v Nombre à arrondir * @param scale Nombre de décimales à conserver après la virgule * @return Le résultat arrondi */ 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(); } };
Voici la totalité du contenu de cet article, j'espère qu'il vous sera utile dans vos études, et j'espère que vous soutiendrez également le tutoriel d'alarme.
Déclaration : le contenu de cet article est extrait du réseau, propriété de l'auteur original, contribué et téléversé par les utilisateurs d'Internet, ce site ne détient pas de droits de propriété, n'a pas été traité par l'édition humaine et n'assume aucune responsabilité juridique connexe. Si vous trouvez du contenu suspect de violation de droits d'auteur, veuillez envoyer un e-mail à : notice#oldtoolbag.com (veuillez remplacer # par @ lors de l'envoi d'un e-mail pour signaler une violation, et fournir des preuves pertinentes. Une fois vérifié, ce site supprimera immédiatement le contenu suspect de violation de droits d'auteur.