Formatting is Specified but argument is not IFormattable

本文探讨了在编程中如何正确使用格式化方法处理数值和字符串,特别关注了小数点后保留两位的实现及避免字符串误用导致的错误提示。

 

 

private void DeviceSetText(TextBox textBox, string text)
        {
            //处理text的显示值
            if (text != "0")  //小数位后保留2位
            {
                //小数点后保留2位小数
                text = string.Format("{0:0.00}", text);
            }
            textBox.Invoke((MethodInvoker) delegate
            {
                textBox.Text = text;
            });
        }
text = string.Format("{0:0.00}", text);

和下面的问题类似

 

http://stackoverflow.com/questions/2849688/formatting-is-specified-but-argument-is-not-iformattable

string listOfItemPrices = items.ToSemiColonList(item => string.Format("{0:C}", item.Price.ToString()));

By passing item.Price.ToString() to String.Format, you are passing a string, not a decimal.
Since strings cannot be used with format strings, you're getting an error.

You need to pass the Decimal value to String.Format by removing .ToString().

string.Format里面处理的是数字,但是传递了字符串,所以有这个提示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值