Windows 应用商店应用 - C# 格式十进制到货币/货币

本文关键字:货币 应用 十进制 格式 Windows | 更新日期: 2023-09-27 17:55:43

我想将我用math.round(variable, 2)四舍五入的小数格式化为货币格式,因此它将始终将4转换为4.00。我尝试这样做:

    public ProductItem(String itemNo, String description, String unitOfMeasure, decimal unitPriceExclVAT, decimal purchasePrice, decimal margin, int actualStock, String imagePath)
    {
        this.ItemNo = itemNo;
        this.Description = description;
        this.UnitOfMeasure = unitOfMeasure;
        this.UnitPriceExclVAT = Math.Round(unitPriceExclVAT, 2);
        this.PurchasePrice = Math.Round(purchasePrice, 2);
        this.Margin = Math.Round(margin, 2);
        this.ActualStock = actualStock;
        this.ImagePath = imagePath;
    }
    public string ItemNo { get; private set; }
    public string Description { get; private set; }
    public string UnitOfMeasure { get; private set; }
    public decimal UnitPriceExclVAT { get; set; }
    public decimal PurchasePrice { get; set; }
    public decimal Margin { get; set; }
    public int ActualStock { get; private set; }
    public string ImagePath { get; private set; }

                foreach (JsonValue itemValue in groupObject["Items"].GetArray())
                {
                    if (uniqueGroupItemsCount != 36)
                    {
                        JsonObject itemObject = itemValue.GetObject();
                        ProductItem product = new ProductItem(itemObject["ItemNo"].GetString(),
                                                        itemObject["Description"].GetString(),
                                                        itemObject["UnitOfMeasure"].GetString(),
                                                        Convert.ToDecimal(itemObject["UnitPriceExclVAT"].GetString().Replace(',', '.')),
                                                        Convert.ToDecimal(itemObject["PurchasePrice"].GetString().Replace(',', '.')),
                                                        Convert.ToDecimal(itemObject["Margin"].GetString().Replace(',', '.')),
                                                        Convert.ToInt32(itemObject["ActualStock"].GetString().Replace(',', '.')),
                                                        itemObject["ImagePath"].GetString());

                        if (product.Description.ToString().ToLower().Trim().Contains(productItems) || product.ItemNo.ToString().ToLower().Trim().Contains(productItems))
                        {
                            var money = Convert.ToDecimal(string.Format("{0:C}", product.Margin));//here is where it goes wrong, i know i can format it like this, but its not working.
                            product.Margin = money;
                            searchedGroup.Items.Add(product);
                            uniqueGroupItemsCount++;
                        }

上面的代码会给我一个错误。错误是:mscorlib 中发生了类型为"System.FormatException"的异常.dll但未在用户代码中处理

希望你能帮我:)

编辑:它不需要是像 €20.00 这样的货币值,只有 20.00 对我来说就足够了,因为我可以在 XAML 中制作欧元符号。

Windows 应用商店应用 - C# 格式十进制到货币/货币

只使用产品。Margin.ToString("C") 当您想要显示项目时,不能将其存储为小数字段中的货币

如前所述,字符串格式化函数仅在您尝试显示值时才有用。 对于您发布的代码,string.Format("{0:C}", product.Margin)会导致使用货币符号格式化的字符串,这会导致Convert.ToDecimal()调用引发异常。

如果我正确理解您的更新,您唯一关心格式的时间是结果将显示给用户的时间。 在这种情况下,您应该考虑使用值转换器将十进制值转换为格式化字符串以进行显示。