MySQL decimal formatting

Without FORMAT() usage

Author: Kamil Ścisłowski

Tue Apr 03 2018
6
PHP
4
MySQL
2
DQL
2
Doctrine
1
decimal

I’ve bothered myself with writing a MySQL database query to a table with prices for quite a long time. In that table a record for 10$ stores value of 1000 because price can be for example 10.50$ (1050). In DQL I wanted to get the price, so in SELECT I wrote price / 100. It resulted in 10.0000 (what?!) instead of 10.00. I couldn’t use MySQL’s FORMAT() because it was unavailable in DQL. The CSV report, which received values directly, unfortunately showed 10.0000.

Solution

It turned out that instead of doing / 100 you can just do * 0.01.

Loading...
Boldare