【问题标题】:C# format decimal to money/currencyC# 格式十进制到货币/货币
【发布时间】:2014-04-03 01:03: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.dll 中发生了“System.FormatException”类型的异常,但未在用户代码中处理

我希望你能帮助我:)

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

【问题讨论】:

    标签: c# windows-store-apps currency-formatting


    【解决方案1】:

    当您想显示该项目时,只需使用 product.Margin.ToString("C"),您不能将其作为货币存储在小数字段中

    【讨论】:

    • 好吧,我不能,因为我将产品对象绑定到一个可观察的集合以在页面上显示它。(并且由于货币值有一个€符号它是不可能的)它没有需要是一个货币值,只要它需要数字 20 并将 .00 放在它后面,所以我得到 20.00 我可以在 XAML 中制作的欧元符号。
    【解决方案2】:

    如前所述,字符串格式化功能仅在您尝试显示值时有用。在您发布的代码的情况下,string.Format("{0:C}", product.Margin) 生成了一个使用货币符号格式化的字符串,这导致 Convert.ToDecimal() 调用引发了异常。

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

    【讨论】:

    • 你没有错,绝对没有。问题是,我无法将数据转换为字符串。我将 Product 对象绑定到 ObservableCollection。 ObservableCollection 包含所有数据。如果我想将数据转换为字符串。我需要更新 Product 对象。这是不可接受的,因为 product.margin 需要小数,而不是字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2015-10-24
    • 2016-01-24
    • 2012-01-22
    • 1970-01-01
    相关资源
    最近更新 更多