Format number to 2 decimal places

MysqlFormattingDecimal

Mysql Problem Overview


I would like to know how can I output a number with 2 decimal places, without rounding the original number.

For example:

2229,999 -> 2229,99

I already tried:

FORMAT(2229.999, 2)
CONVERT(2229.999, DECIMAL(4,2))

Mysql Solutions


Solution 1 - Mysql

When formatting number to 2 decimal places you have two options TRUNCATE and ROUND. You are looking for TRUNCATE function.

Examples:

Without rounding:

TRUNCATE(0.166, 2)
-- will be evaluated to 0.16

TRUNCATE(0.164, 2)
-- will be evaluated to 0.16

docs: http://www.w3resource.com/mysql/mathematical-functions/mysql-truncate-function.php

With rounding:

ROUND(0.166, 2)
-- will be evaluated to 0.17

ROUND(0.164, 2)
-- will be evaluated to 0.16

docs: http://www.w3resource.com/mysql/mathematical-functions/mysql-round-function.php

Solution 2 - Mysql

Solution 3 - Mysql

How about CAST(2229.999 AS DECIMAL(6,2)) to get a decimal with 2 decimal places

Solution 4 - Mysql

Just use

format(number, qtyDecimals)
sample: format(1000, 2)
result 1000.00

Solution 5 - Mysql

This is how I used this is as an example:

CAST(vAvgMaterialUnitCost.`avgUnitCost` AS DECIMAL(11,2)) * woMaterials.`qtyUsed` AS materialCost

Solution 6 - Mysql

Show as decimal Select ifnull(format(100.00, 1, 'en_US'), 0) 100.0

Show as Percentage Select concat(ifnull(format(100.00, 0, 'en_US'), 0), '%') 100%

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTenzaView Question on Stackoverflow
Solution 1 - MysqljmarceliView Answer on Stackoverflow
Solution 2 - MysqljasonlfunkView Answer on Stackoverflow
Solution 3 - MysqlHituptonyView Answer on Stackoverflow
Solution 4 - MysqlAlessandro GarciaView Answer on Stackoverflow
Solution 5 - MysqlTye LucasView Answer on Stackoverflow
Solution 6 - MysqlGregory BolognaView Answer on Stackoverflow