VB.NET FormatNumber 在 C# 中等效吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:02:26  来源:igfitidea点击:

VB.NET FormatNumber equivalent in C#?

提问by Oded

Is there a C# equivalent for the VB.NET FormatNumberfunction?

VB.NETFormatNumber函数是否有 C# 等效项?

I.e.:

IE:

JSArrayString += "^" + (String)FormatNumber(inv.RRP * oCountry.ExchangeRate, 2);

采纳答案by John

In both C# and VB.NET you can use either the .ToString()function or the String.Format()method to format the text.

在 C# 和 VB.NET 中,您都可以使用.ToString()函数或String.Format()方法来格式化文本。

Using the .ToString() method your example could be written as:

使用 .ToString() 方法,您的示例可以写为:

JSArrayString += "^" + (inv.RRP * oCountry.ExchangeRate).ToString("#0.00")

Alternatively using the String.Format() it could written as:

或者使用 String.Format() 可以写成:

JSArrayString = String.Format("{0}^{1:#0.00}",JSArrayString,(inv.RRP * oCountry.ExchangeRate))

In both of the above cases I have used custom formatting for the currency with # representing an optional place holder and 0 representing a 0 or value if one exists.

在上述两种情况下,我都对货币使用了自定义格式,其中 # 表示可选占位符,0 表示 0 或值(如果存在)。

Other formatting characters can be used to help with formatting such as D2 for 2 decimal places or C to display as currency. In this case you would not want to use the C formatter as this would have inserted the currency symbol and further separators which were not required.

其他格式化字符可用于帮助格式化,例如 D2 表示 2 个小数位或 C 显示为货币。在这种情况下,您不希望使用 C 格式化程序,因为这会插入货币符号和其他不需要的分隔符。

See "String.Format("{0}", "formatting string"};" or "String Format for Int" for more information and examples on how to use String.Format and the different formatting options.

有关如何使用 String.Format 和不同格式选项的更多信息和示例,请参阅“ String.Format("{0}", "formatting string"};” 或“ String Format for Int”。

回答by d91-jal

You can use string formatters to accomplish the same thing.

您可以使用字符串格式化程序来完成同样的事情。

double MyNumber = inv.RRP * oCountry.ExchangeRate;
JSArrayString += "^" + MyNumber.ToString("#0.00");

回答by DonkeyMaster

Yes, the .ToString(string) methods. For instance,

是的,.ToString(string) 方法。例如,

int number = 32;
string formatted = number.ToString("D4");
Console.WriteLine(formatted);
// Shows 0032

Note that in C# you don't use a number to specify a format, but you use a character or a sequence of characters. Formatting numbers and dates in C# takes some minutes to learn, but once you understand the principle, you can quickly get anything you want from looking at the reference.

请注意,在 C# 中,您不使用数字来指定格式,而是使用字符或字符序列。在 C# 中格式化数字和日期需要几分钟的时间来学习,但是一旦你理解了原理,你就可以从参考资料中快速获得任何你想要的东西。

Here's a couple MSDN articles to get you started :

这里有几篇 MSDN 文章可以帮助您入门:

Standard Numeric Format StringsFormatting Types

标准数字格式字符串格式类型

回答by Jonathan Allen

While I would recommend using ToString in this case, always keep in mind you can use ANY VB.Net function or class from C# just by referencing Microsoft.VisalBasic.dll.

虽然我建议在这种情况下使用 ToString,但请始终记住,您可以通过引用 Microsoft.VisalBasic.dll 来使用 C# 中的任何 VB.Net 函数或类。