Table of Contents
DECIMAL type
Home Database Mysql Tutorial What type does mysql use for decimals?

What type does mysql use for decimals?

Dec 01, 2021 pm 04:41 PM
mysql decimal

Mysql decimal available types: 1. FLOAT type, which can store single-precision floating-point numbers; 2. DOUBLE type, which can store double-precision floating-point numbers; 3. DECIMAL type, used to store precise values, such as accounting Currency data in the system.

What type does mysql use for decimals?

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

Floating point numbers and fixed point numbers are used to represent decimals in MySQL.

There are two floating-point types, single-precision floating-point numbers (FLOAT) and double-precision floating-point numbers (DOUBLE); there is only one fixed-point type, which is DECIMAL.

Both floating-point types and fixed-point types can be represented by (M, D), where M is called precision, indicating the total number of digits; D is called scale, indicating the number of decimal digits.

The value range of floating point number type is M (1~255) and D (1~30, and cannot be greater than M-2), which represent the display width and number of decimal places respectively. M and D are optional in FLOAT and DOUBLE, and the FLOAT and DOUBLE types will be saved to the maximum precision supported by the hardware. The default D value for DECIMAL is 0 and M value is 10.

The following table lists the decimal types and storage requirements in MySQL.

What type does mysql use for decimals?

The DECIMAL type is different from FLOAT and DOUBLE. DOUBLE is actually stored in the form of a string. The possible maximum value range of DECIMAL is the same as DOUBLE, but the effective value range is determined by M and D. If M is changed and D is fixed, the value range will become larger as M becomes larger.

As can be seen from the above table, the storage space of DECIMAL is not fixed, but is determined by the precision value M, occupying M 2 bytes.

The value range of the FLOAT type is as follows:

  • The signed value range: -3.402823466E 38~-1.175494351E-38.

  • Unsigned value range: 0 and -1.175494351E-38~-3.402823466E 38.

The value range of the DOUBLE type is as follows:

  • The signed value range: -1.7976931348623157E 308~ -2.2250738585072014E-308.

  • Unsigned value range: 0 and -2.2250738585072014E-308~-1.7976931348623157E 308.

Note: Whether it is a fixed-point or floating-point type, if the user-specified precision exceeds the precision range, it will be rounded for processing.

FLOAT and DOUBLE will default to the actual precision when the precision is not specified. DECIMAL will default to (10, 0) if the precision is not specified.

The advantage of floating-point numbers over fixed-point numbers is that they can represent a larger range when the length is constant; the disadvantage is that it can cause accuracy problems.

DECIMAL type

DECIMALThe data type is used to store precise values ​​in the database. We often use the DECIMAL data type for columns that retain exact precision, such as currency data in accounting systems.

To define a column with data type DECIMAL, use the following syntax:

column_name  DECIMAL(P,D);
Copy after login

In the above syntax:

  • P represents the precision of the number of significant digits. P range is 1~65.
  • D represents the number of digits after the decimal point. The range of D is 0~30. MySQL requires D to be less than or equal to (<=)P.

DECIMAL(P,D)Indicates that the column can store P digits of D decimal places. The actual range of a decimal column depends on precision and scale.

Like the INT data type, the DECIMAL type also has UNSIGNED and ZEROFILL attributes. If the UNSIGNED attribute is used, the column for DECIMAL UNSIGNED will not accept negative values.

If ZEROFILL is used, MySQL will pad the display value to 0 to display the width specified by the column definition. Additionally, if we use ZERO FILL on the DECIMAL column, MySQL will automatically add the UNSIGNED attribute to the column.

The following example uses a column called amount defined using the DECIMAL data type.

amount DECIMAL(6,2);
Copy after login

In this example, the amount column can store up to 6 digits with a decimal place of 2; therefore, ## The #amount column ranges from -9999.99 to 9999.99.

MySQL allows the following syntax:

column_name DECIMAL(P);
Copy after login

This is equivalent to:

column_name DECIMAL(P,0);
Copy after login

In this case, the column does not contain a decimal part or decimal point.

Also, we can even use the following syntax.

column_name DECIMAL;
Copy after login

In this case, the default value of

P is 10.

MySQL DECIMAL STORAGE

MySQL分别为整数和小数部分分配存储空间。 MySQL使用二进制格式存储DECIMAL值。它将9位数字包装成4个字节。

对于每个部分,需要4个字节来存储9位数的每个倍数。剩余数字所需的存储如下表所示:

剩余数字
00
1–21
3–42
5–63
7-94

例如,DECIMAL(19,9)对于小数部分具有9位数字,对于整数部分具有19位= 10位数字,小数部分需要4个字节。 整数部分对于前9位数字需要4个字节,1个剩余字节需要1个字节。DECIMAL(19,9)列总共需要9个字节。

MySQL DECIMAL数据类型和货币数据

经常使用DECIMAL数据类型的货币数据,如价格,工资,账户余额等。如果要设计一个处理货币数据的数据库,则可参考以下语法 -

amount DECIMAL(19,2);
Copy after login

但是,如果您要遵守公认会计原则(GAAP)规则,则货币栏必须至少包含4位小数,以确保舍入值不超过$0.01。 在这种情况下,应该定义具有4位小数的列,如下所示:

amount DECIMAL(19,4);
Copy after login

MySQL DECIMAL数据类型示例

首先,创建一个名为test_order的新表,其中包含三列:iddescriptioncost

CREATE TABLE test_order (
    id INT AUTO_INCREMENT PRIMARY KEY,
    description VARCHAR(255),
    cost DECIMAL(19,4) NOT NULL
);
Copy after login

第二步,将资料插入test_order表。

INSERT INTO test_order(description,cost)
VALUES(&#39;Bicycle&#39;, 500.34),(&#39;Seat&#39;,10.23),(&#39;Break&#39;,5.21);
Copy after login

第三步,从test_order表查询数据。

SELECT * from test_order
Copy after login
Copy after login

第四步,更改cost列以包含ZEROFILL属性。

ALTER TABLE test_order
MODIFY cost DECIMAL(19,4) zerofill;
Copy after login

第五步,再次查询test_order表。

SELECT * from test_order
Copy after login
Copy after login

查询结果

如上所见,在输出值中填充了许多零。

因为zerofill,当我们插入负值会报错:

INSERT INTO test_order(description,cost)
VALUES(&#39;test&#39;, -100.11);
提示:
[SQL]INSERT INTO test_order(description,cost)
VALUES(&#39;test&#39;, -100.11)

[Err] 1264 - Out of range value for column &#39;cost&#39; at row 1
Copy after login

其它插入测试结论:

当数值在其取值范围之内,小数位多了,则四舍五入后直接截断多出的小数位。

若数值在其取值范围之外,则直接报Out of range value错误。

【相关推荐:mysql视频教程

The above is the detailed content of What type does mysql use for decimals?. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles