[英]SQL Server ROUND not working
I have a table where one column is Price
( decimal(18,9)
) and another Volume
( bigint
). 我有一张表,其中一列是
Price
( decimal(18,9)
)和另一Volume
( bigint
)。
I am multiplying both values and then applying round
function but nothing works. 我将两个值相乘,然后应用
round
函数,但没有任何效果。
I want it to be 2 decimal place precision. 我希望它是2小数位精度。 How to do it?
怎么做?
SELECT
CAST((Price * Volume) AS decimal(38,2)) test1,
ROUND((Price * Volume), 2) 'VolumePrice',
CONVERT(DOUBLE PRECISION, (Price * Volume)) 'test2'
FROM a
Table values are something like this: 表值是这样的:
Price Volume
-------------------------
63.380000000 131729
63.380000000 61177
44.860000000 246475
44.860000000 246475
44.860000000 63937
97.990000000 84620
191.650000000 438821
I want to simply multiply the price by the volume, to get a total value amount. 我只想将价格乘以数量即可得出总价值。
ROUND()
just changes the decimal value up or down, doesn't change the data type precision. ROUND()
只是向上或向下更改十进制值,而不会更改数据类型的精度。
What you want is to convert to DECIMAL
with a scale of 2. 您想要以2的比例转换为
DECIMAL
。
SELECT
CONVERT(DECIMAL(18,2), Price * Volume) AS DecimalConversion
FROM
A
Converting a decimal of higher scale ( Price * Volume
) to a lower one will automatically round the last digit: 将小数位数(
Price * Volume
)转换为小数位数将自动舍入最后一位:
SELECT
CONVERT(DECIMAL(18,2), '1.901999'), -- 1.90
CONVERT(DECIMAL(18,2), '1.909999'), -- 1.91
CONVERT(DECIMAL(18,2), '1.905999'), -- 1.91
CONVERT(DECIMAL(18,2), '1.904999') -- 1.90
When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence 当运算符组合两个具有不同数据类型的表达式时,数据类型优先级规则会指定将优先级较低的数据类型转换为优先级较高的数据类型
source : MSDN docs 来源 : MSDN文档
In SQL Server precedence order for data types in question is : 在SQL Server中,有关数据类型的优先顺序为:
So bigint is converted to implicitly converted to decimal. 因此bigint被转换为隐式转换为十进制。
If you need your desired results you should simply do 如果您需要理想的结果,则只需执行
SELECT
VolumePrice= cast(Price * Volume as decimal(18,2) )
FROM a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.