As other comments and answers suggest, you have some basics to understand first. I may be saying some things you already know, but bear with me:
- Your
textBox7.Text
contains a string
, not a decimal
.
- If you want a
decimal
for calculations, you have to convert it (I think you already got this far)
- Since
res
is a decimal, whenever you want to look at its value SOMETHING will convert it to a string
. Whether that's you writing it to the Console
or your debugger when you mouse over it. That conversion will use your current Regional Settings. This is why you always see a comma.
- To show it to somebody else or write it somewhere with the format YOU want, you'll have to specify a format or a CultureInfo.
a) Standard Format. Example:
Console.WriteLine(res.toString("F2"));
This will format 123456 with 2 numbers after the decimal point: 123456.00
b) Custom Format. Example:
Console.WriteLine(res.toString("[##-##-##]"));
This will output 123456 to something like [12-34-56]
c) CultureInfo. Example:
Console.WriteLine(res.ToString(CultureInfo.CreateSpecificCulture("nl-BE")));
This will output 1234.56 like in Belgium: with a comma 1234,56
Incidentally, I think en-GB also outputs to a comma :-)
d) Combine. Go nuts! Do both ! Example:
Console.WriteLine(res.ToString("F2", CultureInfo.CreateSpecificCulture("nl-BE")));
formats 123456 to 123456,00 !
Based on that I'd suggest the following:
decimal res = Convert.ToDecimal(textBox7.Text, new CultureInfo("en-GB"));
This assumes your users enter the number using a comma (since the en-GB decimal separator is a comma). If not, use the correct CultureInfo like Invariant of en-US.
res
is adecimal
, not a string. So it can't have a format.0:0.0
even mean for adecimal
? What is the meaning of the:
?0
th parameter instring.Format
.