Sates
Sates

Reputation: 408

Float values round itselves in MySQL

I've just encountered quite a weird situation.

I'm trying to pass a float/double value (320.25 and 660.60) through form to save it in MySQL database. Field prepared for these values is set as double (6.0).

The thing is, when I put 320.25 - only 320 is saved. When I put 660.60 - suddenly 667 is saved in that particular field. I even tried to do that with a comma (,) but the result is unchanged.

What might be the problem? I'm not using round() or anything like that anywhere.

Thanks for any help.

Upvotes: 0

Views: 118

Answers (3)

Vickel
Vickel

Reputation: 7997

you can use either decimal(6,2) or double(6,2) the first number represents all digits, the second the decimals

Upvotes: 1

Marc B
Marc B

Reputation: 360622

Simple. You told mysql to only display 6 digits in your float:

mysql> create table foo (
    dbl1 double(6,0),
    dbl2 double,
    dbl3 double(6,2));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into foo (dbl1, dbl2, dbl3) values (1.234567, 1.234567, 1.234567);
Query OK, 1 row affected (0.00 sec)

mysql> select * from foo;
+------+----------+------+
| dbl1 | dbl2     | dbl3 |
+------+----------+------+
|    1 | 1.234567 | 1.23 |
+------+----------+------+
1 row in set (0.00 sec)

Note how the 6,0 field displays NO fractional digits, and how the 6,2 float display 2 fractional digits.

Upvotes: 1

Salketer
Salketer

Reputation: 15711

in MySQL double, according to manual: double(m,d) is: "m" = to number total of digits, and d = number of decimals.

So with your setup, You'd like to use either double(6,2) to hold max 9999,99 or double(8,2) for 999999,99

Upvotes: 2

Related Questions