How to convert a string to a currency with two decimal places in C #?

How to format the following result in a currency with two decimal places?

string query2 = "SELECT SUM (Price) FROM Bill";
OleDbDataAdapter dAdapter2 = new OleDbDataAdapter(query2, DBconn);
DataTable source = new DataTable();
dAdapter2.Fill(source);
TotalValueLabel.Text = source.Rows[0][0].ToString();
+3
source share
2 answers
TotalValueLabel.Text = source.Rows[0][0].ToString("c");
+3
source

Some overloads ToStringaccept format strings that allow you to specify the output configuration (this depends on the class). For a currency (for example, stored in decimal), you probably want to specify currencyValue.ToString("C")which takes into account the default locale.

Note. You can also pass the Locale qualifier if you need results in other currencies.

See http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx for details .

+1
source

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


All Articles