sql server 四舍六入五留双
时间: 2023-11-06 17:06:53 浏览: 53
SQL Server中的四舍六入五留双可以通过以下代码实现:
<<SQL>>
CREATE FUNCTION [dbo].[RoundHalfEven](@num float, @i int)
RETURNS varchar(50)
AS
BEGIN
DECLARE @str varchar(50)
DECLARE @str1 varchar(1)
DECLARE @str2 varchar(50)
DECLARE @str3 varchar(50)
SET @str = convert(varchar,@num)
IF charindex('.',@str) = 0
RETURN @str
SET @str1 = substring(@str,charindex('.',@str) + 1+@i,1)
SET @str2 = reverse(substring(reverse(@str),1,charindex('.',reverse(@str)) - 2 - @i))
SET @str3 = substring(@str,charindex('.',@str) + 1,@i)
IF @str1 = 5 and convert(int,@str2) = 0 and @str3%2 = 0 /*如果符合"五成双"*/
SET @str = substring(@str,1,charindex('.',@str) + @i)
ELSE /*否则四舍五入*/
SET @str = convert(varchar,round(@num,@i))
SET @str = substring(@str,1,charindex('.',@str) + @i)
RETURN @str
END
<<SQL>>
其中,@num表示要进行四舍六入五留双的数值,@i表示要保留的小数位数。
相关推荐
![sql](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)