BigDecimal Usage Summary

BigDecimal It is often used to calculate the amount , Here's a summary of BigDecimal Usage of .

1. Add, subtract, multiply and divide

2. Setting accuracy

3. Take the opposite

Add, subtract, multiply and divide, respectively

[java] view plain copy

2. public BigDecimal subtract(BigDecimal value);
3. public BigDecimal multiply(BigDecimal value);
4. public BigDecimal divide(BigDecimal value);

give an example ：

[java] view plain copy

1. BigDecimal a=new BigDecimal(10.0);
2. BigDecimal b=new BigDecimal(9.1);
3. System.out.println(a.subtract(b));

Precision setting , Why set precision , Let's see an effect

The code is as follows ：

[java] view plain copy

1. BigDecimal abig=new BigDecimal(10.0);
2. BigDecimal bbig=new BigDecimal(9.1);
3. BigDecimal cbig=new BigDecimal(8.9);
4. System.out.println(abig.subtract(bbig));
5. System.out.println(abig.subtract(cbig));

give the result as follows ：

[java] view plain copy

1. 0.9000000000000003552713678800500929355621337890625
2. 1.0999999999999996447286321199499070644378662109375

It's not what we want to see 0.9 and 1.1, The reason is that there will be precision problems when converting to binary , The result is . So we can add precision to the calculation , You can also instantiate BigDecimal When you use strings .

How to set the precision ：

[java] view plain copy

1. System.out.println(abig.subtract(bbig).setScale(2, BigDecimal.ROUND_HALF_UP));
2. System.out.println(abig.subtract(cbig).setScale(2, BigDecimal.ROUND_HALF_UP));

It's OK to set the two bit precision in this way

[java] view plain copy

1. 0.90
2. 1.10

String instantiation method ：

[java] view plain copy

1. BigDecimal abig=new BigDecimal("10.0");
2. BigDecimal bbig=new BigDecimal("9.1");
3. BigDecimal cbig=new BigDecimal("8.9");

Take the opposite

because Bigdecimal It can't be used directly +-*/ These symbols are used to calculate , Therefore, a separate method is needed to implement the negation ：

[java] view plain copy

1. System.out.println(abig.negate());

So you'll get the opposite of it ：

[java] view plain copy

1. -10.0

These are the points used in this project

## BigDecimal More articles on Usage Summary

1. BigDecimal Usage details （ turn ）

BigDecimal Usage details     http://www.cnblogs.com/linjiqin/p/3413894.html One . brief introduction Java stay java.math Provided in the package API class BigDecim ...

2. BigDecimal Usage details

BigDecimal brief introduction BigDecimal usage : BigDecimal Construction method of BigDecimal Common method description BigDecimal Compare BigDecimal summary BigDecimal brief introduction J ...

3. BigDecimal Usage details

One . brief introduction Java stay java.math Provided in the package API class BigDecimal, Used for more than 16 Bit effective The number of bits is precisely calculated . Double precision floating point variables double Can handle 16 Bit significant number . in application , Need to be bigger or more ...

4. Study BigDecimal usage

One . brief introduction Java stay java.math Provided in the package API class BigDecimal, Used for more than 16 The number of significant bits of a bit is calculated precisely . Double precision floating point variables double Can handle 16 Bit significant number . in application , Need to be bigger or more ...

5. JAVA BigDecimal usage

One .BigDecimal Addition, subtraction, multiplication and division BigDecimal bignum1 = new BigDecimal("10"); BigDecimal bignum2 = new Big ...

6. BigDecimal In short

1) The discard rule of floating point numbers : Suppose we keep two places after the decimal point RoundingMode.CEILING: Round in the direction of positive infinity :  1.245 → 1.25   -1.245 → -1.24 RoundingMode ...

7. Java Basic knowledge of 【 Next 】( Reprint )

http://blog.csdn.net/silentbalanceyh/article/details/4608360 ( Finally decided to write a new one Java Basic content , Because I didn't write this chapter ...

8. JAVA How to put a float Round to the decimal point 2 position ,4 position , Or other specified digits .

How to make float Keep two or more decimal places http://meryvn.blog.163.com/blog/static/36962664201173010402629/ The two methods : import   j ...

9. Java Decimal operation and precision loss in

float.double Type of question We all know , Computers use binary system to store data . And in everyday life , Most of the time we use the decimal system , So most of what the computer shows us is also decimal , This makes a lot of time the data needs to be in ...

## Random recommendation

1. Jquery Paging function

Jquery Code /// <reference path="jquery-1.9.1-vsdoc.js" />// Anchor point var anchor="#apage&q ...

2. linux-7 man command

man   Classification of commands man command Code It represents the content A common order Functions and tools called by the kernel Common functions and function libraries Description of the device documentation The configuration file game Conventions and agreements Commands available to administrators Kernel related files In general, help documentation ...

3. Alphabetic arrangement _next_permutation_ Lexicographic functions _ To be solved

problem B: Alphabetic arrangement The time limit : 1 Sec   Memory limit : 64 MB Submit : 19   solve : 5[ Submit ][ state ][ Discussion board ] Title Description When given a string of characters , We can change the characters one by one , Form a new string . If you're interested in this ...

4. sql Splicing strings and converting types

select top 10 a.ID as a_ID,a.Name as a_Name,c.* from (select * from DC_Trees where Pid=187 or ID=187 ...

5. 【 Zero base learning iOS Development 】【01- Preface 】01- The opening

Contents of this article One . What is? iOS Two . Mainstream mobile operating systems 3、 ... and . What is? iOS Development Four . Study iOS The purpose of the development 5、 ... and . Study iOS The premise of development Starting today , I started updating [ Zero base learning iOS Development ] This topic . Whether you've been involved or not I ...

6. Knockout, Web API and ASP.Net Web Forms Simple data binding

Use Knockout, Web API and ASP.Net Web Forms Simple data binding   Original address :http://www.dotnetjalps.com/2013/05/Simple-da ...

7. android Study ProgressBar Simple use

android Provided ProgressBar There are two types of controls , One is the progress bar without progress , One is the progress bar with progress , If you can calculate the amount of tasks completed, then use... With progress bar , If you can't calculate the amount of tasks , Then use a progress bar without progress .Pr ...

8. JUnit Unit testing problems and solutions

JUnit yes Java Unit test framework , When we unit test the developed system , Also encountered the problem of how to test multiple test cases .   background : All our test cases are stored in Excel In file , This file contains test cases and expected output . We hope ...