Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to use number format in a MySQL query?

Tags:

mysql

I need to set the "Total Price" value to be a two decimal point value like "56.35". Now it's showing fraction values like "56.3566666". I need it to be formatted by MySQL "SELECT" query.

like image 580
Prabhu M Avatar asked May 18 '10 06:05

Prabhu M


2 Answers

select
    format(field, 2) as formatted
from
    table

Do note that Format() returns a string, and the result will be with two decimal places (in the above example) - i.e. 100 will be formatted as 100.00.

Documentation.

like image 165
Björn Avatar answered Oct 19 '22 22:10

Björn


That works too, but if you need it for further calculations or what not AND you have MySQL > 5.0.8 you could also try:

select
    cast(field as decimal(14, 2)) as formatted
from
    table

It is a bit more flexible this way! I like flexible...

like image 39
MasterJeev Avatar answered Oct 20 '22 00:10

MasterJeev