简体   繁体   English

“方法“ ToString”的无重载需要1个参数”

[英]“no overload for method “ToString” takes 1 arguments”

I would like to convert the data as is down to money formatting, but it is giving error. 我想将数据直接转换为货币格式,但出现错误。

Faturamento.ValorNF = decimal.Parse(RsFaturamento.Fields["ValorTotal"].Value.ToString("#.##"));

Error Message: "no overload for method “ToString” takes 1 arguments" 错误消息:“方法“ ToString”的无重载采用1个参数”

I wonder what's wrong with my code? 我想知道我的代码有什么问题吗?

Whatever type Value is make sure it has a public override string ToString(){} method which gives you the string representation. 无论Value类型如何,请确保其具有公共替代字符串ToString(){}方法,该方法为您提供字符串表示形式。

Then you can do: 然后,您可以执行以下操作:

decimal money = decimal.Parse(RsFaturamento.Fields["ValorTotal"].Value.ToString());

Faturamento.ValorNF = money.ToString("C"));

Whatever the type of object Value is, the class definition for it doesn't have an overload of ToString() that takes an argument. 无论对象Value是什么类型,其类定义都不会带有参数的ToString()重载。 If Value is of type object in the collection, you'll need to cast/convert it to a string that is then supplied to the decimal's parse method. 如果Value是集合中的object类型,则需要将其强制转换/转换为字符串,然后将其提供给小数的parse方法。 Note that the ToString("#.##") doesn't really matter if you're assigning a decimal object to ValorNF as you can always format it at a later time for display. 请注意,如果要为ValorNF分配一个十进制对象,则ToString(“#。##”)并不重要,因为您始终可以在以后的时间对其进行格式化以进行显示。

That being said, if you wanted to format a decimal to 2 decimal places, I'd suggest you use a standard format string for doing so. 话虽如此,如果您想将小数点格式设置为2个小数位,建议您使用标准格式字符串。 Below will parse the value into a decimal and then use the ToString to convert it to a string to 2 decimal places: 下面将值解析为十进制,然后使用ToString将其转换为2位小数的字符串:

decimal.Parse(RsFaturamento.Fields["ValorTotal"].Value.ToString()).ToString("N2", CultureInfo.InvariantCulture);

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

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