我有一个小问题,要获取小数点后的6位数字而不截断。
示例:
输入:
id amt
2 256.25356890
输出:
id amt
2 256.253568
即amt
列不应四舍五入:
id amt
2 256.253569 (this round-off should not happen)
答案 0 :(得分:0)
在SQL Server中使用ROUND(256.25356890, 6, 1)
和TRUNC(256.25356890,6)
在Oracle中
答案 1 :(得分:0)
declare @X float = 2256.25356890;
SELECT ROUND(@X * 1000000.0, 0) / 1000000.0;
答案 2 :(得分:0)
解决您的问题
只需运行此功能
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create FUNCTION DecimalWithOutRoundingOff
(
@DecimalInput varchar(50)
)
RETURNS varchar(100) -- or whatever length you need
AS
BEGIN
DECLARE @start INT, @end INT,@num1 varchar(100),@num2 varchar(100)
SELECT @start = 1, @end = CHARINDEX('.', @DecimalInput)
WHILE @start < LEN(@DecimalInput) + 1 BEGIN
IF @end = 0
SET @end = LEN(@DecimalInput) + 1
set @num2 = Substring(SUBSTRING(@DecimalInput, @start, @end - @start),1,6)
set @num1 = substring(@DecimalInput,1,CHARINDEX('.',@DecimalInput)-1)
SET @start = @end + 1
SET @end = CHARINDEX('.', @DecimalInput, @start)
END
return @num1 +'.'+ @num2
END
GO
要使用:
select dbo.DecimalWithOutRoundingOff(256.25356890)
结果:
256.253568