T-SQL:将钱转换为浮动

时间:2017-08-21 09:41:15

标签: sql-server tsql type-conversion

如何将(money/int)*100转换为float数据类型?

a.ResidualValue的类型为money,而ListPriceCar的类型为int

以下输出一个以逗号作为小数点分隔符的数字,因此我认为它仍然是money字段,而不是float我想要的字段:

CONVERT(float, CONVERT(float, REPLACE(CONVERT(nvarchar(10), a.ResidualValue), ',', '.'))/ListPriceCar)*100 AS 'Residual value %'

结果:62,825

3 个答案:

答案 0 :(得分:0)

简单演员的问题如下:

DECLARE @money AS money
SET @money = $2345234.35
SELECT @money, CAST(@money AS float)

答案 1 :(得分:0)

ResidualValue的价值是多少?如果ResidualValue包含逗号,则逗号是您的十进制字符。你不应该把它转换成一个点。您可以将货币值除以int值。

declare @a money = 10.5;
declare @b int = 20;

select (@a / @b) * 100
select cast((@a / @b) * 100 as float)

答案 2 :(得分:0)

在您的示例中,结果已作为浮点数给出。您可以通过运行以下示例代码来测试它。

--INPUT VARIABLES
declare @ResidualValue money = 5000.54
declare @ListPriceCar int = 15000
--OUTPUT VARIABLE
declare @ResultCalculation as sql_variant;
--CALCULATION
set @ResultCalculation = CONVERT(float, CONVERT(float, REPLACE(CONVERT(nvarchar(10), @ResidualValue), ',', '.'))/@ListPriceCar)*100 
--GET INFORMATION ON THE OUTPUT VARIABLE
select sql_variant_property(@ResultCalculation,'BaseType') AS 'Base Type',
       sql_variant_property(@ResultCalculation,'Precision') AS 'Precision',
       sql_variant_property(@ResultCalculation,'Scale') AS 'Scale',
       sql_variant_property(@ResultCalculation,'TotalBytes') AS 'TotalBytes',
       sql_variant_property(@ResultCalculation,'Collation') AS 'Collation',
       sql_variant_property(@ResultCalculation,'MaxLength') AS 'MaxLength';

enter image description here

有一种更简单的方法来计算你的答案:

CAST(@ResidualValue/@ListPriceCar * 100 as float)