我想格式化这样的数字:
从CentOS Linux release 7.2.1511 (Core)
到65803.12345
。
从65803.12
到65803.98500
。
答案 0 :(得分:1)
如果你想得到小数点后只有两位数,没有天花板或地板。
DECLARE @i AS float
DECLARE @TmpInt AS INT
DECLARE @TmpDec AS FLOAT
SELECT @i = 65803.12345
SELECT @TmpInt = (SELECT CAST(@i AS INT))
SELECT @TmpDec = @i - @TmpInt
SELECT CAST(((CAST(@TmpInt AS VARCHAR(50)) + SUBSTRING((LEFT((CAST(@TmpDec AS VARCHAR(50))), 4)), 2, 3))) AS DECIMAL(10, 2))
-- Output : 65803.12
SELECT @i = 65803.98500
SELECT @TmpInt = (SELECT CAST(@i AS INT))
SELECT @TmpDec = @i - @TmpInt
SELECT CAST(((CAST(@TmpInt AS VARCHAR(50)) + SUBSTRING((LEFT((CAST(@TmpDec AS VARCHAR(50))), 4)), 2, 3))) AS DECIMAL(10, 2))
-- Output : 65803.98
SELECT @i = 65803.12666
SELECT @TmpInt = (SELECT CAST(@i AS INT))
SELECT @TmpDec = @i - @TmpInt
SELECT CAST(((CAST(@TmpInt AS VARCHAR(50)) + SUBSTRING((LEFT((CAST(@TmpDec AS VARCHAR(50))), 4)), 2, 3))) AS DECIMAL(10, 2))
-- Output : 65803.12
答案 1 :(得分:0)
答案 2 :(得分:0)
试试这个
declare @s decimal(12,5)
set @s = 65803.98500
select cast(@s-@s%1+cast(@s%1*100 as int)*0.01 as decimal(12,2))
结果是
65803.98