简体   繁体   English

SQL ROUND()函数问题

[英]SQL ROUND() function issue

I have tried to use round function to convert for example 348.426580 to 348.43 我试图使用round函数将例如348.426580转换为348.43
But in below query I used the result I got is 348.430000 in [ShippingCost] column 但是在下面的查询中,我使用了[ShippingCost]列中的结果为348.430000

How can I omit the four zeros? 如何省略四个零?

SELECT  S.Product_Name, 
        SPD.UnitPrice, 
        SPD.Quantity, 
        SPD.Quantity * SPD.UnitPrice Amount,
        CONVERT(INT,(SPD.Quantity * SPD.UnitPrice)/(SUM(SPD.Quantity * SPD.UnitPrice) OVER()) * 100) [Cost %],
        ROUND((SPD.Quantity * SPD.UnitPrice)/(SUM(SPD.Quantity * SPD.UnitPrice) OVER()) * Sp.Shipping_Cost,2) [ShippingCost],
        (SPD.Quantity * SPD.UnitPrice)/(SUM(SPD.Quantity * SPD.UnitPrice) OVER()) * Sp.Customs_Cost [CustomsCost],
        (SPD.Quantity * SPD.UnitPrice)/(SUM(SPD.Quantity * SPD.UnitPrice) OVER()) * Sp.Shipping_Cost + (SPD.Quantity * SPD.UnitPrice)/(SUM(SPD.Quantity * SPD.UnitPrice) OVER()) * Sp.Customs_Cost +SPD.Quantity * SPD.UnitPrice - SpD.Discount [TotalAmount]
FROM dbo.Stock_Purchase SP
INNER JOIN dbo.Stock_Purchase_Details SPD
    ON SP.Purchase_ID = SPD.Purchase_ID 
INNER JOIN dbo.Store S
    ON SPD.Pro_ID = S.Pro_ID;

You can cast to a decimal, say DECIMAL(10, 2) . 您可以将其DECIMAL(10, 2)转换为小数,例如DECIMAL(10, 2) Because casting automatically does rounding, there is no need for round() : 因为强制转换会自动进行舍入,所以不需要round()

    CAST((SPD.Quantity * SPD.UnitPrice)/(SUM(SPD.Quantity * SPD.UnitPrice) OVER()) * Sp.Shipping_Cost as decimal(10, 2)) as [ShippingCost],
ROUND(column_name,decimals)

column_name Required for The field to round. column_name该字段是必需的。

decimals Required for Specifies the number of decimals to be returned. 必需的小数位数指定要返回的小数位数。

See this. 看到这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM