Home Java javaTutorial What is BigDecimal? Create an instance of BigDecimal

What is BigDecimal? Create an instance of BigDecimal

Jun 25, 2017 am 10:40 AM
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
Copy after login

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
Copy after login

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));
    }
Copy after login

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
Copy after login

BigDecimal.compareTo(BigDecimal val)

When BigDecimal compares Returns 1 when val is large,

Returns -1 when BigDecimal is less than val,

Returns 0 when BigDecimal is equal to val.

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Java BigDecimal class application example code analysis Java BigDecimal class application example code analysis Apr 20, 2023 pm 06:25 PM

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

How BigDecimal compares size How BigDecimal compares size Dec 06, 2023 pm 01:44 PM

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.

How to use the formatting method of java BigDecimal How to use the formatting method of java BigDecimal May 14, 2023 pm 07:25 PM

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

How to solve BigDecimal exception in java How to solve BigDecimal exception in java Apr 26, 2023 pm 10:49 PM

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.

How to use BigDecimal in java How to use BigDecimal in java Apr 18, 2023 pm 09:46 PM

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

What is the role of BigDecimal in java What is the role of BigDecimal in java May 05, 2023 pm 05:13 PM

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 exact number division Java uses the divide() function of the BigDecimal class to implement exact number division Jul 25, 2023 pm 01:46 PM

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

Analysis of Java large number BigInteger and BigDecimal class examples Analysis of Java large number BigInteger and BigDecimal class examples Apr 27, 2023 pm 10:40 PM

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

See all articles