mysql - 将数字格式化为 2 位小数

我想知道如何在不四舍五入的情况下输出一个小数点后 2 位的数字。

例如:

2229,999 -> 2229,99

我已经试过了:

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

最佳答案

将数字格式化为 2 位小数时,您有两个选项 TRUNCATEROUND。您正在寻找 TRUNCATE 函数。

例子:

不四舍五入:

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

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

文档:https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_truncate

四舍五入:

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

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

文档:https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_round

https://stackoverflow.com/questions/11190668/

相关文章:

java - 如何在java中将当前日期转换为字符串?

windows - 将换行格式从 Mac 转换为 Windows

visual-studio - 在 Visual Studio 中关闭自动格式化

r - 如何在R中将数字格式化为百分比?

text - UIButton 中的文本下划线

javascript - 如果一千或更多,则将数字格式化为 2.5K,否则为 900

python - 转换为二进制并保留前导零

asp.net-mvc - 日期格式不正确的 MVC DateTime 绑定(bind)

sql-server - 向 SQL Server Reporting Services 报表添加交

python - Python中的货币格式