如何在 SQLServer 中显示小数点后两位数字

我有一个表,其中有一列 float数据类型在 SQL Server 我想返回小数点后两位的 float数据类型列值。

如果我插入 12.3,它应该返回 12.30

如果我插入 12,它应该返回 12.00

598007 次浏览
select cast(your_float_column as decimal(10,2))
from your_table

decimal(10,2) means you can have a decimal number with a maximal total precision of 10 digits. 2 of them after the decimal point and 8 before.
The biggest possible number would be 99999999.99

You can also use below code which helps me:

select convert(numeric(10,2), column_name) as Total from TABLE_NAME

where Total is alias of the field you want.

You can also Make use of the Following if you want to Cast and Round as well. That may help you or someone else.

SELECT CAST(ROUND(Column_Name, 2) AS DECIMAL(10,2), Name FROM Table_Name

You can also do something much shorter:

SELECT FORMAT(2.3332232,'N2')

select cast(56.66823 as decimal(10,2))

This returns 56.67.