2014-04-01 203 views
1

我想格式化一個小數,我用math.round(變量,2)四捨五入到一個貨幣格式,所以它總是會將4轉換爲4.00。我試圖這樣做:C#格式十進制貨幣/貨幣

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

上面的代碼會給我一個錯誤。 錯誤是:類型System.FormatException「的異常出現在mscorlib.dll,但在用戶代碼中沒有處理

我希望你能幫助我:)

編輯:不需要是20.00歐元的貨幣值,只有20.00對我來說足夠好,因爲我可以在XAML中做歐元符號。

回答

1

只使用product.Margin.ToString(「C」)時要顯示的項目,你不能將其存儲爲一個貨幣小數場

+0

嗯,我不能,因爲我將產品對象綁定到一個可觀察的集合,以便在頁面上顯示它(並且由於貨幣值具有€符號,所以它不可能)它不需要是貨幣價值,只要它需要數字20並把.00放在後面,所以我得到20.00歐元符號,我可以在XAML中做出。 – Proliges

0

如前所述,字符串格式化功能是當唯一有用您正試圖顯示該值。在您發佈代碼的情況下,string.Format("{0:C}", product.Margin)會導致使用貨幣符號格式化的字符串,從而導致Convert.ToDecimal()調用引發異常。

如果我正確理解您的更新,那麼您唯一關心格式的時間是結果將顯示給用戶的時間。在這種情況下,您應該考慮使用value converter將十進制值轉換爲格式化字符串以進行顯示。

+0

你沒有錯,絕對不是。問題是,我無法將數據轉換爲字符串。 我將Product對象綁定到ObservableCollection。 ObservableCollection包含所有的數據。如果我想將數據轉換爲字符串。我需要更新Product對象。這是不可接受的,因爲product.margin需要一個小數,而不是一個字符串。 – Proliges