当前位置:网站首页>Accuracy problem and solution of BigDecimal
Accuracy problem and solution of BigDecimal
2022-07-05 02:53:00 【Bubble ^ bubble】
BigDecimal take %( remainder )
package smallspring.step02;
import java.math.BigDecimal;
public class BigDecimalTest {
public static void main(String[] args) {
double a1 = 3.3D;
double a2 = 0.1D;
System.out.println("a1%a2 Residual results :"+a1 % a2);
}
}
terms of settlement : Use BigDecimal, Do not use new BigDecimal(), Use BigDecimal.valueof().
System.out.println(" Use new BigDecimal:"+new BigDecimal(a1));
System.out.println(" Use BigDecimal.valueOf:"+BigDecimal.valueOf(a1));
divideAndRemainder: Returns the first integer in the array set , The second is the remainder , This method can be used to avoid the loss of accuracy .
BigDecimal[] values = BigDecimal.valueOf(a1).divideAndRemainder(BigDecimal.valueOf(a2));
Tool class
package smallspring;
import java.math.BigDecimal;
/** * because Java The simple type of can't operate on floating-point numbers accurately , This utility class provides precise floating-point operations , It includes addition, subtraction, multiplication, division and rounding . */
public class DoubleUtil {
// Default division precision
private static final int DEF_DIV_SCALE = 10;
/** * Provide precise addition operations . * @param v1 Augend * @param v2 Addition number * @return The sum of two parameters */
public static double add(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.add(b2).doubleValue();
}
/** * Provides accurate subtraction operations . * @param v1 minuend * @param v2 Subtract * @return The difference between the two parameters */
public static double sub(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.subtract(b2).doubleValue();
}
/** * Provide accurate multiplication . * @param v1 Multiplier * @param v2 The multiplier * @return The product of two parameters */
public static double mul(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.multiply(b2).doubleValue();
}
/** * Provide ( relative ) A precise division operation , When there is an inexhaustible situation , Accurate to * After the decimal point 10 position , The next figures are rounded off to . * @param v1 Divisor * @param v2 Divisor * @return The quotient of two parameters */
public static double div(double v1, double v2) {
return div(v1, v2, DEF_DIV_SCALE);
}
/** * Provide ( relative ) A precise division operation . When there is an inexhaustible situation , from scale Parameter refers to * Set the accuracy , The next figures are rounded off to . * @param v1 Divisor * @param v2 Divisor * @param scale The representation needs to be accurate to several decimal places . * @return The 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 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/** * Provide accurate decimal rounding . * @param v A number that needs to be rounded * @param scale How many decimal places to keep * @return The result of rounding */
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 = BigDecimal.valueOf(v);
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
边栏推荐
- El select, El option drop-down selection box
- Sqoop installation
- Utilisation simple de devtools
- ELK日志分析系统
- Day_ 17 IO stream file class
- Avoid material "minefields"! Play with super high conversion rate
- Apache Web page security optimization
- Hmi-31- [motion mode] solve the problem of picture display of music module
- d3js小记
- 单项框 复选框
猜你喜欢
【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)
2. Common request methods
Single box check box
IPv6 experiment
【LeetCode】222. The number of nodes of a complete binary tree (2 mistakes)
Pytest (4) - test case execution sequence
Acwing game 58 [End]
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Idea inheritance relationship
Apache Web page security optimization
随机推荐
Design of kindergarten real-time monitoring and control system
2021 Li Hongyi machine learning (3): what if neural network training fails
Single box check box
TCP security of network security foundation
Good documentation
Design and practice of kubernetes cluster and application monitoring scheme
Zabbix
Three line by line explanations of the source code of anchor free series network yolox (a total of ten articles, which are guaranteed to be explained line by line. After reading it, you can change the
Vb+access hotel service management system
El select, El option drop-down selection box
Character painting, I use characters to draw a Bing Dwen Dwen
When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
Comparison of advantages and disadvantages between platform entry and independent deployment
Dart series: collection of best practices
Character painting, I use characters to draw a Bing Dwen Dwen
【LeetCode】501. Mode in binary search tree (2 wrong questions)
Design of KTV intelligent dimming system based on MCU
Design and implementation of high availability website architecture
端口,域名,协议。