如何将十进制值拆分为两个十进制值。 如果小数部分小于.50或大于.50 .it的十进制数应该以这样的方式分割,即第一个不应该仅以.00或.50结尾。第二个值应包含剩余的阶乘值。
ex. 19.97 should return 19.50 & 0.47
19.47 19.00 & 0.47
答案 0 :(得分:3)
你可以通过乘以2来“降低”到0.5的最高倍数,调用FLOOR
,然后除以2.从那里只从原始值中减去得到余数。
DECLARE @test decimal(10,7)
SELECT @test =19.97
SELECT
FLOOR(@test * 2) / 2 AS base,
@test - FLOOR(@test * 2) / 2 AS fraction
或减少重复
SELECT
base,
@test - base AS fraction
FROM ( SELECT FLOOR(@test * 2) / 2 AS base )
答案 1 :(得分:1)
Declare @money money
Set @money = 19.97
Select convert(int,@money - (@money % 1)) as 'LeftPortion'
,convert(int, (@money % 1) * 100) as 'RightPortion'
答案 2 :(得分:1)
观察加倍0.5
给出1
,这是一个整数。这导致了一个简单的算法:
floor(x)
和x-floor(x)
我们以你的数字为例:
19.97 * 2 = 39.94
Whole part = 39, fractional part = 0.94
Dividing each part by 2 individually, we get
39/2 = 19.50
0.94/2 = 0.47
19.47 * 2 = 38.94
Whole part = 38, fractional part = 0.94
Dividing each part by 2 individually, we get
38/2 = 19.00
0.94/2 = 0.47
答案 3 :(得分:0)
您需要查看MODULO
operator in SQL Server.
SELECT
19.97 as myDecVal ,
19.97 % 0.5 AS decGreaterThan50,/*should return 0.47*/
19.97- (19.97 % 0.5) as roundedToNearestZeroPoint5 /*should return 19.50*/;