Proliges
Proliges

Reputation: 361

C# format decimal to money/currency

i want to format a decimal that i rounded with math.round(variable, 2) to a money format, so it will always convert 4 to 4.00. I tried to do it like this:

    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++;
                        }

above code will provide me with an error. the error is: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

I hope you can help me :)

Edit: It doesn't need to be a currency value like €20.00 only 20.00 is good enough for me, since i can make the euro sign in the XAML.

Upvotes: 1

Views: 2457

Answers (2)

David Smith
David Smith

Reputation: 1

As mentioned, the string formatting function is only useful when you are attempting to display the value. In the case of the code you posted,string.Format("{0:C}", product.Margin) resulted in a string formatted with the currency symbol, which caused the Convert.ToDecimal() call to throw an exception.

If I understand your update correctly, the only time you are concerned with the formatting is when the result will be displayed to the user. In this case, you should look into using a value converter to convert the decimal value to a formatted string for display.

Upvotes: 0

Matt Tabor
Matt Tabor

Reputation: 1053

just use product.Margin.ToString("C") when you want to DISPLAY the item, you cant store it as a currency in a decimal field

Upvotes: 1

Related Questions