Home Database Mysql Tutorial mysql decimal_MySQL

mysql decimal_MySQL

Jun 01, 2016 pm 01:29 PM
double Money

bitsCN.com

mysql decimal

 

首先,对于精度比较高的东西,比如money,我会用decimal类型,不会考虑float,double,因为他们容易产生误差,

 

DECIMAL列的声明语法是DECIMAL(M,D)。在MySQL 5.1中,参量的取值范围如下: 

 

·         M是数字的最大数(精度)。其范围为1~65(在较旧的MySQL版本中,允许的范围是1~254)。 

 

·         D是小数点右侧数字的数目(标度)。其范围是0~30,但不得超过M。 

 

说明:float占4个字节,double占8个字节,decimail(M,D)占M+2个字节。 

 

如DECIMAL(5, 2) 的最大值为9 9 9 9 . 9 9,因为有7 个字节可用。 

 

译者注: 

 

M 与D 对DECIMAL(M, D) 取值范围的影响 

 

类型说明 取值范围(MySQL = 3.23) 

 

 

代码如下  

 

DECIMAL(4, 1) -9.9 到 99.9 -999.9 到 9999.9    DECIMAL(5, 1) -99.9 到 999.9 -9999.9 到 99999.9    DECIMAL(6, 1) -999.9 到 9999.9 -99999.9 到 999999.9    DECIMAL(6, 2) -99.99 到 999.99 -9999.99 到 99999.99    DECIMAL(6, 3) -9.999 到 99.999 -999.999 到 9999.999 
Copy after login

# 在MySQL 3.23 及以后的版本中,DECIMAL(M, D) 的取值范围等于早期版本中的DECIMAL(M + 2, D) 的取值范围。

还有一种数据类型也是比较棒的: 那就是LongBlob,这种数据类型可以直接把图像文件存到数据库中!

笔者,最近在研究mysql的decimal数据类型,现把数据实验结果公布如下

数据库版本:Server version: 5.0.45 Source distribution

1、创建表结构

代码如下

create table ta (a float,b decimal(10,5)); 
Copy after login

2、插入数据

代码如下

insert into ta (a,b) values(1,12345.123423); 
Copy after login

实际插入的b列数据为:12345.12342

代码如下

insert into ta (a,b) values(1,123456.1234); 
Copy after login

 

 

 

实际插入的b列数据为:99999.99999 

 

结论:decimal数据类型, 

 

1、当插入的整数部分的值超过了其表示范围后就直接忽略了小数部分的值,并以最大值填充。 

 

2、当整数部分合法,小数部分多余的位数,直接截断。

bitsCN.com
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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

C++ program to convert double type variable to int type C++ program to convert double type variable to int type Aug 25, 2023 pm 08:25 PM

In C++, variables of type int can only hold positive or negative integer values; they cannot hold decimal values. There are float and double values ​​available for this purpose. The double data type was created to store decimals up to seven digits after the decimal point. Conversion of an integer to a double data type can be done automatically by the compiler (called an "implicit" conversion), or it can be explicitly requested by the programmer from the compiler (called an "explicit" conversion). In the following sections, we'll cover various conversion methods. Implicit conversions The compiler performs implicit type conversions automatically. To achieve this, two variables are required - one of floating point type and the other of integer type. When we simply assign a floating point value or variable to an integer variable, the compiler takes care of all the other things

How many bytes does double occupy? How many bytes does double occupy? Dec 25, 2020 am 10:36 AM

The bytes occupied by double: 1. Under a 16-bit compiler, double occupies 8 bytes; 2. Under a 32-bit compiler, double occupies 8 bytes; 3. Under a 64-bit compiler, double occupies 8 bytes .

What is the difference between float and double What is the difference between float and double Oct 11, 2023 pm 05:38 PM

The main differences between float and double lie in precision, storage and calculation speed, range, and use in programming languages. Detailed introduction: 1. The precision is different. Float is a single-precision floating point number, occupying 4 bytes (32 bits), while double is a double-precision floating point number, occupying 8 bytes (64 bits); 2. The storage and calculation speed are different. , double takes up more space and requires more storage space to store values. In applications that require high performance and speed, it may be more efficient to use the float type; 3. Different ranges, etc.

How to use the isInfinite() method of the Double class to determine whether a number is infinitesimal How to use the isInfinite() method of the Double class to determine whether a number is infinitesimal Jul 26, 2023 am 11:04 AM

How to use the isInfinite() method of the Double class to determine whether a number is infinitesimal. Infinite small is a concept in mathematics that is used to indicate that a number is close to zero but not equal to zero. In computer programming, we often need to determine whether a number is infinitesimal. Java's Double class provides the isInfinite() method to determine whether a number is infinitesimal. This article will introduce how to use the isInfinite() method of the Double class to determine whether a number is infinitesimal and give

Interpretation of Java documentation: Analysis of the function of the valueOf() method of the Double class Interpretation of Java documentation: Analysis of the function of the valueOf() method of the Double class Nov 04, 2023 am 10:55 AM

Interpretation of Java documentation: Analysis of the function of the valueOf() method of the Double class, which requires specific code examples. Introduction: In Java development, the conversion and operation of digital types are often involved. The Double class is a wrapper class in Java that provides some practical methods to handle double-precision floating point operations. Among them, the valueOf() method is an important method in the Double class. This article will analyze the function of this method and provide specific code examples to help readers better understand. one,

How to use the isPositiveInfinity() method of the Double class to determine whether a number is positive infinity How to use the isPositiveInfinity() method of the Double class to determine whether a number is positive infinity Jul 24, 2023 pm 07:57 PM

How to use the isPositiveInfinity() method of the Double class to determine whether a number is positive infinity Introduction In Java programming, we often need to make some judgments and processes on numerical values. Sometimes we need to determine whether a number is positive infinity. Java's Double class provides an isPositiveInfinity() method, which can be used to determine whether a number is positive infinity. This article will introduce how to use this method and some related considerations. Do

Java Iterator vs. Iterable: Improve your understanding of Java collections Java Iterator vs. Iterable: Improve your understanding of Java collections Feb 19, 2024 pm 06:20 PM

Sets can be divided into two categories: ordered sets and unordered sets. The data items in an ordered set are arranged in a certain order, while the data items in an unordered set have no fixed order. The Java collection framework provides a rich set of collection classes that can easily store and manage data. Commonly used collection classes in the Java collection framework include: List: list collection, ordered collection, supporting repeated elements. Set: Set, unordered collection, does not support repeated elements. Map: dictionary collection, unordered collection, key-value pair storage. Understand the concepts and types of collections. Master the use of collection classes. Ability to use collection classes to store and manage data. Understand the concepts and types of collections. Master the use of collection classes. Ability to use collection classes to store and manage data. Understand the concept of sets

Interpretation of Java documentation: Functional analysis of compare() method of Double class Interpretation of Java documentation: Functional analysis of compare() method of Double class Nov 04, 2023 pm 12:25 PM

Interpretation of Java documentation: Analysis of the function of the compare() method of the Double class. The Double class is a wrapper class used to represent double-precision floating point numbers (64 bits) in Java. It provides a range of methods for manipulating and comparing numbers. One of the commonly used methods is compare(), which is used to compare the values ​​of two Double objects. This article will provide a detailed analysis of the compare() method of the Double class and provide corresponding code examples to help readers better understand its functions and usage.

See all articles