11

i try to format a decimal as 0:0.0 in c# tried this code

string nv = textBox7.Text.Trim().Replace(',', '.');

res =  Convert.ToDecimal(nv, new CultureInfo("en-GB"));

but res always show a result with a comma, tried also with

new CultureInfo("en-GB")

but the problem persist And thank you in advance.

5
  • res is a decimal, not a string. So it can't have a format. Apr 21, 2012 at 10:51
  • 2
    What does 0:0.0 even mean for a decimal? What is the meaning of the :?
    – Oded
    Apr 21, 2012 at 10:51
  • 1
    @Oded I guess it stands for 0th parameter in string.Format. Apr 21, 2012 at 10:54
  • @CodeInChaos - It's a good guess and what I also assume, but would like the OP to explain, since it is not entirely clear.
    – Oded
    Apr 21, 2012 at 10:55
  • What the debugger shows for the value for res has nothing to do with the way it will be stored in the dbase. Make sure your dbase column is numeric and not text. Apr 21, 2012 at 11:37

3 Answers 3

20

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:

  1. Your textBox7.Text contains a string, not a decimal.
  2. If you want a decimal for calculations, you have to convert it (I think you already got this far)
  3. 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.
  4. 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.

3

res is a decimal, not a string. So it can't have a format. Decimals are pure mathematical numbers without an associated format. The format only comes into existence when you convert a decimal to a string.

You can use res.ToString(CultureInfo.InvariantCulture) to produce a string that uses . as decimal separator.

6
  • i would like to have a decimal to insert it in my database,
    – tarek
    Apr 21, 2012 at 11:10
  • @user And where is your problem? Apr 21, 2012 at 11:18
  • the problem is solved, i thought that i should use a decimal variable to use it an sqlCommand instruction, but also a string can make the same think, i used string nv = textBox7.Text.Trim().Replace(',', '.');
    – tarek
    Apr 21, 2012 at 11:22
  • 1
    @user1348165 I don't understand you, but Replace(',', '.') is certainly not the right answer. Apr 21, 2012 at 11:26
  • possible you want decimal.ToString(decimal.Parse(input,someCulture), CultureInfo.InvariantCulture), but you're so vague that I don't understand your actual problem. Apr 21, 2012 at 11:27
0

This will get you the number decimal with no commas like "15000.00":

FormatNumber(nv, , , , 0)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.