如果我们少于2个小数点,则如何显示最大2个小数点,它应在SQL Server中显示实际值

时间:2018-10-10 15:54:11

标签: sql sql-server stored-procedures

示例:

  • 10应该显示10
  • 10.3应该显示10.3
  • 10.50应该显示10.50
  • 10.246应显示10.24
  • 10.2568应显示10.25
  • 10.38754应该显示10.38等

如果小数点后有两位以上的数字,则应截断。

我必须在存储过程中实现它。前端应用程序只有exe,无法更改。

2 个答案:

答案 0 :(得分:2)

很明显,这属于“表示层”,但是如果您必须...

示例

Declare @YourTable table (Value decimal(16,6))
Insert Into @YourTable values
(10.00)
,(10.3)
,(10.53)
,(10.246)

Select *
      ,NewString = format(round(Value,2,1),'0.##')
 From @YourTable

返回

Value       NewString
10.000000   10
10.300000   10.3
10.530000   10.53
10.246000   10.24   -- Note not rounded to 10.25

答案 1 :(得分:0)

只需使用FORMAT函数

declare @value float
set @value = 10.245


select format(@value,'0.0#') as formatted_value

将显示10.26

如果要截断,则速度更快:

select round(@value,2,1)

将显示10.25