繁体   English   中英

从SQL将十进制格式转换为Money C#Aspx

[英]Format decimal to money c# aspx from sql

嗨,我在我的.cs文件中有此代码,输出为100449.00,但我希望它格式化为金钱,如100,449.00。 这是我的代码,用于显示标签中的值。

billing.Text = "$" + ds.Tables[0].Rows[0]["Billing"].ToString();
billing.Text = "$" + ds.Tables[0].Rows[0]["Billing"].ToString("N");

这个

编辑:所返回的是一个object ,你需要转换为十进制。 尝试:

((decimal)ds.Tables[0].Rows[0]["Billing"]).ToString("N");
billing.Text = ds.Tables[0].Rows[0]["Billing"].ToString("c");

来自DataTable要求我们在将其格式化为文本之前转换为不可为null的类型。

实际上,我们有几种不同的格式化方法。 您的帖子在值之前有一个硬编码的美元符号,在这种情况下,我们可以使用F2N2格式的字符串为我们提供一个小数点,在右边保留2个位置,并将其附加到您所拥有的美元符号上:

billing.Text = "$" +  ((decimal)ds.Tables[0].Rows[0]["Billing"]).ToString("F2");
// 123456.7890 will display as $123456.78

billing.Text = "$" +  ((decimal)ds.Tables[0].Rows[0]["Billing"]).ToString("N2");
// 123456.7890 will display as $123,456.78

另一种选择是使用C格式,该格式将为我们添加特定于文化的货币符号和数字格式(小数点,逗号)

billing.Text = ((decimal)ds.Tables[0].Rows[0]["Billing"]).ToString("C");
// 123456.7890 will display as
//    $123,456.78   en-US
//    123 456.78€   fr-FR
// you could also add a second overload to the ToString to specify

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM