繁体   English   中英

C#货币格式

[英]C# Currency format

如何以货币格式显示以下代码? 添加“ c”对我不起作用。

totalLabel.Text = cmd.ExecuteScalar().ToString();

您可能需要将ExecuteScalar的值放入适当的数字类型的字段中,然后执行

myAppropriatelyTypeField.ToString("c");

ExecuteScalar()方法返回类型为System.Object的值,因此您需要将其System.Object转换为数字(十进制,双精度或整数)。

object value = cmd.ExecuteScalar();
if(value!=null)
{
  decimal num=(decimal)value;
  totalLabel.Text=num.ToString("C2")
}

也许这会帮助您:

decimal total = 0;
object cmdValue = cmd.ExecuteScalar();
if (cmdValue != null && decimal.TryParse(cmdValue.ToString(), out total))
    totalLabel.Text = String.Format("{0:C}", total);

您可能要为此使用string.format

var stringValue = String.Format("{0:C}", yourValue);

希望能有所帮助

totalLabel.Text = String.Format("{0:C}", cmd.ExecuteScalar());

暂无
暂无

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

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