How to use BigDecimal in Java to keep a two-bit valid number for a double type

Source: Internet
Author: User

One, scene: read from the data table decimal type of data is directly plugged into a double type of object, there is no exception.

If you want to calculate on this basis again, an exception will occur.

For example: read out the data is 0.0092, multiply it by 100, then become 0.919999999999999 ...

Second, Reason:

The Java MySQL data types are compared as follows:

Type name Display length Database type Java type JDBC Type index (int) Describe
VARCHAR L+n VARCHAR Java.lang.String 12
CHAR N CHAR Java.lang.String 1
Blob L+n Blob Java.lang.byte[] -4
TEXT 65535 VARCHAR Java.lang.String -1
INTEGER 4 INTEGER UNSIGNED Java.lang.Long 4
TINYINT 3 TINYINT UNSIGNED Java.lang.Integer -6
SMALLINT 5 SMALLINT UNSIGNED Java.lang.Integer 5
Mediumint 8 Mediumint UNSIGNED Java.lang.Integer 4
BIT 1 BIT Java.lang.Boolean -7
BIGINT 20 BIGINT UNSIGNED Java.math.BigInteger -5
FLOAT 4+8 FLOAT Java.lang.Float 7
DOUBLE 22 DOUBLE Java.lang.Double 8
DECIMAL 11 DECIMAL Java.math.BigDecimal 3
BOOLEAN 1 With tinyint
Id 11 PK (INTEGER UNSIGNED) Java.lang.Long 4
DATE 10 DATE Java.sql.Date 91
Time 8 Time Java.sql.Time 92
Datetime 19 Datetime Java.sql.Timestamp 93
TIMESTAMP 19 TIMESTAMP Java.sql.Timestamp 93
Year 4 Year Java.sql.Date 91






Three, the solution:

1, the double type re-bigdecimal type, the final result also to obtain a significant number of digits

Double d = 111231.5585;

BigDecimal B = new BigDecimal (f);

Double df = B.setscale (2, bigdecimal.round_half_up). Doublevalue ();

2, because the type of the data table is a decimal, so the corresponding member variables in the Java object type is changed to BigDecimal.

Iv. introduction of BigDecimal

In MySQL, for high-precision data storage, such as money, the decimal type is required, not the float or double type, because the latter has a large data error.

The declaration syntax for the decimal column is decimal (m,d).

In MySQL 5.1, the value range of the parameter:

1, M is the maximum number (precision) of the number. The range is 1~65 (in the older MySQL version, the allowable range is 1~254).

2, D is the number of digits to the right of the decimal point (scale). Its scope is 0~30, but must not exceed M.
Description: Float accounted for 4 bytes, double was 8 bytes, decimail (m,d) accounted for m+2 bytes.
The maximum value for decimal (5, 2) is 9 9 9 9. 9 9, because there are 7 bytes available.

Note:
The effect of M and D on the range of decimal (M, d) values

Type description value range (MySQL < 3.23) range (MySQL >= 3.23)
DECIMAL (4, 1)-9.9 to 99.9-999.9 to 9999.9
DECIMAL (5, 1)-99.9 to 999.9-9999.9 to 99999.9
DECIMAL (6, 1)-999.9 to 9999.9-99999.9 to 999999.9
DECIMAL (6, 2)-99.99 to 999.99-9999.99 to 99999.99
DECIMAL (6, 3)-9.999 to 99.999-999.999 to 9999.999


# in MySQL 3.23 and later versions, the value range of decimal (M, d) equals the range of decimal (M + 2, D) in earlier versions.

Another type of data:
Longblob, this type of data can be directly stored in the data table of the image file!

In the study of MySQL's decimal data type, data experiment results are now published:
Database version: Server version:5.0.45 Source Distribution

1. CREATE TABLE structure

CREATE Table Ta (a float,b decimal (10,5));

2. Inserting data

INSERT into TA (A, B) values (1,12345.123423);

The column B data actually inserted is: 12345.12342

INSERT into TA (A, B) values (1,123456.1234);

The column B data actually inserted is: 99999.99999

Conclusion: The decimal data type,
1. When the value of the inserted integer part exceeds its representation, the value of the fractional part is ignored and populated with the maximum value.
2, when the integer part of the legal, fractional part of the extra digits, directly truncated.

How to use BigDecimal in Java to keep a two-bit valid number for a double type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.