What is BigDecimal? Create an instance of BigDecimal
The float and double types are mainly designed for scientific calculations and engineering calculations. They perform binary floating point operations and are carefully designed to provide more accurate approximate calculations over a wide range of values. of. However, they do not provide completely accurate results and should not be used where accurate results are required. Usually business calculations often require BigDecimal to calculate values with high accuracy requirements. For example,
0.07 + 0.020.58 - 0.421.005 * 10015.1 / 1000
No matter in any environment, the code needs to be converted to a binary machine The code can be recognized by the machine. When the floating point number is output directly, the accuracy will be maintained. However, when the floating point number is calculated, the accuracy may be lost. In this case, BigDecimal is needed for calculation.
1. Create BigDecimal
BigDecimal has a constructor that takes a double-precision floating point number as a parameter. In fact, when floating point numbers are passed in for calculation, the accuracy will still be lost.
= BigDecimal(1.005= BigDecimal(100= BigDecimal("1.005"= BigDecimal("100"100.49999999999998934185896359849721193313598632812500 100.500
But you can find that when you pass in a string, you get accurate results, and BigDecimal.valueOf(double val) can also get accurate results. You can see the source code of BigDecimal.valueOf(double val) below
public static BigDecimal valueOf(double val) {// Reminder: a zero double returns '0.0', so we cannot fastpath// to use the constant ZERO. This might be important enough to// justify a factory approach, a cache, or a few private// constants, later.return new BigDecimal(Double.toString(val)); }
This method first converts the parameters to String type, and then calls the constructor parameters whose parameters are String type, so when using BigDecimal, try to use new BigDecimal(String val) or BigDecimal .valueof(double val) to ensure accurate results.
2.compareTo
## Use compareTo() to compare the size of BigDecimal.
0.05).compareTo(BigDecimal.valueOf(0.040.04).compareTo(BigDecimal.valueOf(0.040.03).compareTo(BigDecimal.valueOf(0.04
The above is the detailed content of What is BigDecimal? Create an instance of BigDecimal. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. Introduction The main design goal of float and double types is for scientific calculations and engineering calculations. They perform binary floating-point operations, which are carefully designed to provide fast approximate calculations with high accuracy over a wide range of values. However, they do not provide completely accurate results and should not be used where accurate results are required. However, business calculations often require accurate results, and BigDecimal comes in handy at this time. 2. Introduction to knowledge points 1. Overview 2. Construction methods 3. Addition, subtraction, multiplication and division operations 4. Description of source code 5. Summary 6. Refining exercises 3. Detailed explanation of knowledge points 1. Overview Why use BigDecimal? Code demonstration: packagecom.Test;import

Methods for comparing sizes: 1. Use the "compareTo()" method; 2. Use the "equals()" method; 3. Use the overloaded version of the "compareTo()" method; 4. Use the chain of the "compareTo()" method Call etc.

1. The description takes using BigDecimal to format currency and percentage as an example. First, create a BigDecimal object, perform BigDecimal arithmetic operations, and establish currency and percentage formatted references respectively. Finally, use the BigDecimal object as a parameter of the format() method to output its formatted currency value and percentage. 2. Instance NumberFormatcurrency=NumberFormat.getCurrencyInstance();//Create currency formatting reference NumberFormatpercent=NumberFormat.getPercentIns

1. Explain that an exception occurs during division. java.lang.ArithmeticException:Non-terminatingdecimalexpansion;noexactrepresentabledecimalresult When dividing by the divide method of BigDecimal, if there is no integer division and a recurring decimal occurs, an exception will be thrown: java.lang.ArithmeticException:Non-terminatingdecimalexpansion;noexactrepresentabledecimalresult.

1. BigDecimal uses scale() to represent the number of decimal places. BigDecimald1=newBigDecimal("987.65");BigDecimald2=newBigDecimal("987.6500");BigDecimald3=newBigDecimal("98765400");System.out.println(d1.scale());//2, represents two decimal places System. out.println(d2.scale());//4Sy

1. Explain that the API class BigDecimal provided by Java in the java.math package is used to accurately calculate more than 16 significant digits. Double-precision floating-point variables can handle 16-bit significands, but in practical applications, larger or smaller numbers may need to be calculated and processed. 2. Instance BigDecimala=newBigDecimal(0.1);System.out.println("avaluesis:"+a);System.out.println("==================== ===");BigDecimalb=

Java uses the divide() function of the BigDecimal class to implement accurate numerical division. When performing numerical calculations, especially when division operations need to retain decimal precision, there will be a problem of precision loss when using floating point numbers (float or double). To solve this problem, Java provides the BigDecimal class, which can perform high-precision number operations. The BigDecimal class is a class provided by Java for representing high-precision numbers. It can accurately represent finite decimals

The BigInteger class in Java is a class that has many numbers to process, such as the Integer class, but the Integer class also has an upper limit. Its maximum value is 2^31-1. If we want to represent a larger number at this time, it cannot be represented by Integer, so Java provides the BigInteger class. The numbers supported by the BigInteger class can be said to be infinite, and it supports integers with arbitrary precision, which means that it can accurately represent any value without loss. It should also be emphasized here that because the number type passed in is a character type, you cannot use +-*/ when doing operations. The corresponding method is to use them: add
