Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store decimal in MySQL?

Tags:

mysql

I've tried using DECIMAL with (2,2) but it won't let me use this.

I simply want to store a number, for example 7.50 or 10.50. I need to keep both numbers after the decimal though but when I refresh the database it resets the values to 0.99. Any suggestions?

like image 866
K20GH Avatar asked May 18 '13 22:05

K20GH


People also ask

Can MySQL store decimal int?

MySQL DECIMAL storageMySQL assigns the storage for integer and fractional parts separately. MySQL uses binary format to store the DECIMAL values. It packs 9 digits into 4 bytes. For example, DECIMAL(19,9) has 9 digits for the fractional part and 19-9 = 10 digits for integer part.

How do you store decimals?

The database server uses one byte of disk storage to store two digits of a decimal number, plus an additional byte to store the exponent and sign, with the first byte representing a sign bit and a 7-bit exponent in excess-65 format. The rest of the bytes express the mantissa as base-100 digits.

How do I get decimal points in MySQL?

MySQL FORMAT() function MySQL FORMAT() converts a number to a format like '#,###,###. ##' which is rounded upto the number of decimal places specified (in the second argument) and returns the result as a string.


2 Answers

The first parameter of the DECIMAL declaration is the total digits. You probably want to use DECIMAL (4, 2). This allows for up to two digits before the decimal and two after.

Documentation: https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html

like image 166
Explosion Pills Avatar answered Sep 19 '22 08:09

Explosion Pills


The syntax is DECIMAL(M,D)

M - total length

D - digits right of the decimal point

http://dev.mysql.com/doc/refman/5.6/en/fixed-point-types.html

The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.6 are as follows:

M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)

D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

like image 43
hol Avatar answered Sep 21 '22 08:09

hol