我想将我用 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 中使用欧元符号。