舍入到小数点后两位不起作用

时间:2022-01-23 15:54:43

I am trying to reduce the decimal places of my number to two. Unfortunately is not possible. For this reason I added some of my code, maybe you will see the mistake...

我试图将我的数字的小数位数减少到两位。不幸的是不可能。出于这个原因,我添加了一些代码,也许你会看到错误......

Update [dbo].[company$Line] SET
Amount = ROUND((SELECT RAND(1) * Amount),2),
...
SELECT * FROM [dbo].[company$Line]

Amount in db which I want to change:

我要更改的数据库中的数量:

0.00000000000000000000
1914.65000000000010000000
376.81999999999999000000
289.23000000000002000000

Result I get after executing the code:

结果我在执行代码后得到:

0.00000000000000000000
1366.28000000000000000000
268.89999999999998000000
206.38999999999999000000

Result I want to get (or something like this):

结果我想得到(或类似的东西):

0.00000000000000000000                  or 0.00
1366.30000000000000000000               or 1366.30
268.99000000000000000000                or 268.99
206.49000000000000000000                or 206.49

1 个解决方案

#1


1  

RAND() returns float.
According to data type precedence the result of multiplying decimal and float is float, try:

RAND()返回float。根据数据类型优先级,乘以decimal和float的结果是float,尝试:

ROUND(CAST(RAND(1) as decimal(28,12)) * Amount, 2)

this should do the trick.

这应该可以解决问题。

#1


1  

RAND() returns float.
According to data type precedence the result of multiplying decimal and float is float, try:

RAND()返回float。根据数据类型优先级,乘以decimal和float的结果是float,尝试:

ROUND(CAST(RAND(1) as decimal(28,12)) * Amount, 2)

this should do the trick.

这应该可以解决问题。