Atul Ranjan
Atul Ranjan

Reputation: 557

MySQL datatype float (8, 2) value getting rounded off to one decimal point when fetched in php with Laravel framework

My database has a float(8,2) field and has a value with two decimal points (5.49). While fetching it to php, the value gets rounded to one decimal point (5.5) although I expect it to be to two decimal point (5.49) and not rounded off.

I found a solution by changing the datatype to decimal, but I can not do so for production environment (some reason). How can I restrict the round off?

I am using and and .

Thanks in advance.

Upvotes: 0

Views: 2060

Answers (1)

Mohammed Adnan A K
Mohammed Adnan A K

Reputation: 11

MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE PRECISION(M,D). Here, (M,D) means than values can be stored with up to M digits in total, of which D digits may be after the decimal point. For example, a column defined as FLOAT(7,4) is displayed as -999.9999. MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001.

Upvotes: 1

Related Questions