将数字格式化为2位小数

时间:2021-01-06 16:54:21

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))

Thanks

谢谢

5 个解决方案

#1


71  

You want to use the TRUNCATE command.

您需要使用truncatetable命令。

http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_truncate

http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html function_truncate

#2


287  

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

当将数字格式化为2位小数时,您有两个选项可以截断和四舍五入。您正在寻找截形函数。

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

文档: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

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

#3


8  

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

如何使用CAST(2229.999作为DECIMAL(6,2))来获得一个小数点后两位的小数呢

#4


0  

This is how I used this is as an example:

我就是这么用的这是一个例子

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

#5


0  

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

只需使用format(number, qtyDecimals)示例:format(1000,2)结果1000.00

#1


71  

You want to use the TRUNCATE command.

您需要使用truncatetable命令。

http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_truncate

http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html function_truncate

#2


287  

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

当将数字格式化为2位小数时,您有两个选项可以截断和四舍五入。您正在寻找截形函数。

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

文档: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

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

#3


8  

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

如何使用CAST(2229.999作为DECIMAL(6,2))来获得一个小数点后两位的小数呢

#4


0  

This is how I used this is as an example:

我就是这么用的这是一个例子

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

#5


0  

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

只需使用format(number, qtyDecimals)示例:format(1000,2)结果1000.00