c#输入字符串格式错误

时间:2015-08-08 00:47:56

标签: c# mysql .net

我正在尝试从我的mysql数据库中获取数据。我能够从数据库中获取所有数据,但我想以货币格式格式化我的一个列。但是,当我尝试时,我收到输入字符串错误。

我的代码:

var result = db.Query("SELECT * FROM Requests");
var data = result.Select(x => new 
{
    TimeStamp = Convert.ToString(x.TimeStamp),
    TelephoneNumber = x.TelephoneNumber,
    Make = x.Make,
    Model = x.Model,
    Body_Type = x.Body_Type,
    StockNum = x.StockNumber,
    Price = String.Format("{0:C}",  Int32.Parse(x.Price)),
    InventoryDate = x.Inventory_Date          
}).ToArray();

“价格”行导致输入字符串错误

1 个答案:

答案 0 :(得分:1)

改变这个:

Price = String.Format("{0:C}", String.Format("{0:C}", Int32.Parse(x.Price)))

为:

Price = String.Format("{0:C}", Int32.Parse(x.Price))

看看它是否正常工作?

或者,尝试以下方法:

Price = Int32.Parse(x.Price).ToString("C")

根据x.Price的价值,也可以将其投放到floatdoubledecimal,而不是{{1} }}

相关问题