VB.NET FormatNumber equivalent in C #?

Is there a C # equivalent for a VB.NET function FormatNumber?

i.e:.

JSArrayString += "^" + (String)FormatNumber(inv.RRP * oCountry.ExchangeRate, 2);
+3
source share
4 answers

In both C # and VB.NET you can use either . ToString () or String.Format () Method for formatting text.

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

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

Alternatively, using String.Format (), it can be written as:

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

In both cases, I used custom formatting for a C # currency representing an optional place holder, and 0 representing 0 or a value if it exists.

, D2 2 C, . C, , .

. " String.Format(" {0} "," "};" " String Format for Int" String.Format .

+10

, .

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

, .ToString(string). ,

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

, # , , . # , , , , , .

MSDN, :

Standard Number Format Strings Formatting Types

+2
source

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

-1
source

Source: https://habr.com/ru/post/1696728/


All Articles