gpt4 book ai didi

c# - Kentico 购物车总价问题

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:03 26 4
gpt4 key购买 nike

我正在使用 Kentico API 在我的网站上显示我的产品,但我在显示购物车总价时遇到了问题。它会自动舍入 - 如果价格为 11.5,则为 12。

这是我返回总价的方法:

    public double GetTotalShoppingCart(int userID, string siteName)
{
double totalPrice=0.0;
ShoppingCartInfo cartInfo = GetShopCard(userID, siteName);
int siteID = GetSite(siteName);
if (cartInfo != null)
{
DataSet cartItems = ShoppingCartItemInfoProvider.GetShoppingCartItems(cartInfo.ShoppingCartID);
if (cartItems.Tables[0].Rows.Count > 0)
{
foreach (DataRow row in cartItems.Tables[0].Rows)
{
totalPrice += ShoppingCartItemInfoProvider.GetShoppingCartItemInfo(int.Parse(row["CartItemID"].ToString())).TotalPrice;
}
}
}
return totalPrice;
}

只有当总价是整数或实数时,它才会返回正确的总价,但如果总价包含任何小数,则会将其四舍五入为最大的数字。您知道导致问题的原因吗?

最佳答案

我试过使用decimal,但它也没有正常工作,所以我自己计算了总价,比如:

public decimal GetTotalShoppingCart(int userID, string siteName)
{
decimal totalPrice=0;
ShoppingCartInfo cartInfo = GetShopCard(userID, siteName);
int siteID = GetSite(siteName);

if (cartInfo != null)
{
DataSet cartItems = ShoppingCartItemInfoProvider.GetShoppingCartItems(cartInfo.ShoppingCartID);
if (cartItems.Tables[0].Rows.Count > 0)
{
foreach (DataRow row in cartItems.Tables[0].Rows)
{
totalPrice += decimal.Parse(row["SKUUnits"].ToString()) * decimal.Parse(row["SKUPrice"].ToString());//(decimal)ShoppingCartItemInfoProvider.GetShoppingCartItemInfo(int.Parse(row["CartItemID"].ToString())).TotalPrice;
}
}
}
return totalPrice;

}

非常感谢您的帮助:)

关于c# - Kentico 购物车总价问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37256549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com