当前位置:网站首页>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();
}
}
边栏推荐
- Yyds dry goods inventory intelligent fan based on CC2530 design
- Why are there fewer and fewer good products produced by big Internet companies such as Tencent and Alibaba?
- 2. Common request methods
- El tree whether leaf node or not, the drop-down button is permanent
- Azkaban actual combat
- 腾讯云,实现图片上传
- Azkaban安装部署
- Qrcode: generate QR code from text
- Design and implementation of campus epidemic prevention and control system based on SSM
- spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time
猜你喜欢

Watch the online press conference of tdengine community heroes and listen to TD hero talk about the legend of developers

el-select,el-option下拉选择框

Design and implementation of community hospital information system

Design and practice of kubernetes cluster and application monitoring scheme

Voice chip wt2003h4 B008 single chip to realize the quick design of intelligent doorbell scheme

丸子百度小程序详细配置教程,审核通过。

Can you really learn 3DMAX modeling by self-study?

The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety

Idea inheritance relationship

Elfk deployment
随机推荐
Azkaban actual combat
Hmi-32- [motion mode] add light panel and basic information column
ELK日志分析系统
Pytest (4) - test case execution sequence
tuple and point
Pat class a 1162 postfix expression
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
Can you really learn 3DMAX modeling by self-study?
[Yu Yue education] National Open University spring 2019 0505-22t basic nursing reference questions
openresty ngx_ Lua variable operation
100 basic multiple choice questions of C language (with answers) 04
腾讯云,实现图片上传
Hot knowledge of multithreading (I): introduction to ThreadLocal and underlying principles
Yyds dry goods inventory intelligent fan based on CC2530 design
Single line function*
Daily question 2 12
TCP security of network security foundation
Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
2. Common request methods