Question

In my program on the WindowsForms (VS13) I have a label, which contains a value of the variable:

label6.Text = indicators.money.ToString();

When I'm doing like this, I'm getting a value of the variable money on the screen: 50. But i need to display it like 50$. How to do that without adding one more label?

Was it helpful?

Solution

.Net framework provides a way for showing currency sign with monetary values.

For your case you can do:

label6.Text = indicators.money.ToString("C");

But as Jon Skeet has pointed out in comment:

approach here will use the current culture to determine not only the appropriate currency symbol, but also how/where to display it.

So, if your culture is en-US you will get the currency sign before the value like:

$23.25

You can change that for the culture using properties: NumberFormatInfo.CurrencyPositivePattern Property and NumberFormatInfo.CurrencyNegativePattern Property. Like:

CultureInfo ci = new CultureInfo("en-US");
ci.NumberFormat.CurrencyPositivePattern = 1;
decimal money = 23.25M;
string str = money.ToString("C", ci);

then you will get:

"23.25$"

CurrencyPositivePatter expects an int value which determines the position of currency symbol, For en-US culture it would be like:

0      $n
1      n$
2      $ n //(with a space)
3      n $ //(with a space)

You may see: Standard Numeric Format Strings - Currency Format specifier

The "C" (or currency) format specifier converts a number to a string that represents a currency amount. The precision specifier indicates the desired number of decimal places in the result string. If the precision specifier is omitted, the default precision is defined by the NumberFormatInfo.CurrencyDecimalDigits property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top