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
- public BigDecimal add(BigDecimal value);
- public BigDecimal subtract(BigDecimal value);
- public BigDecimal multiply(BigDecimal value);
- public BigDecimal divide(BigDecimal value);
give an example ：
- BigDecimal a=new BigDecimal(10.0);
- BigDecimal b=new BigDecimal(9.1);
Precision setting , Why set precision , Let's see an effect
The code is as follows ：
- BigDecimal abig=new BigDecimal(10.0);
- BigDecimal bbig=new BigDecimal(9.1);
- BigDecimal cbig=new BigDecimal(8.9);
give the result as follows ：
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 ：
- System.out.println(abig.subtract(bbig).setScale(2, BigDecimal.ROUND_HALF_UP));
- System.out.println(abig.subtract(cbig).setScale(2, BigDecimal.ROUND_HALF_UP));
It's OK to set the two bit precision in this way
String instantiation method ：
- BigDecimal abig=new BigDecimal("10.0");
- BigDecimal bbig=new BigDecimal("9.1");
- 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 ：
So you'll get the opposite of it ：
These are the points used in this project
- 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 ...
- BigDecimal Usage details
BigDecimal brief introduction BigDecimal usage : BigDecimal Construction method of BigDecimal Common method description BigDecimal Compare BigDecimal summary BigDecimal brief introduction J ...
- 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 ...
- 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 ...
- JAVA BigDecimal usage
One .BigDecimal Addition, subtraction, multiplication and division BigDecimal bignum1 = new BigDecimal("10"); BigDecimal bignum2 = new Big ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Jquery Paging function
Jquery Code /// <reference path="jquery-1.9.1-vsdoc.js" />// Anchor point var anchor="#apage&q ...
- 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 ...
- 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 ...
- 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 ...
- 【 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 ...
- 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 ...
- 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 ...
- 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 ...
- download Android Summary of source code compilation errors
error 1: prebuilts/sdk/api/18.txt:22055: error 9: Removed public method android.telephony.gsm.SmsMessage ...
- C++ When do you need NEW object ,new And defining objects
C++ When do you need NEW object One .CString str; no need new The memory area of is in the stack , Will automatically allocate space , It is generally used in local variables , When the function is finished, the space will be released automatically , Defining an object is declaring an object ( static state ) str There is a system to create and interpret ...