0

I have a problem in converting numbers.

I have :

private decimal amount { get; set; }

When I write in a textbox the value 10.15, the conversion is good, it converts me to 10,15

But when I write 10 or 35 it's not 10,00 or 35,00 but the value it's the same 10 and 35

How can I convert it ?

Thanks

Memo
  • 213
  • 3
  • 13

1 Answers1

2

A decimal is just a number - it doesn't have a format.

You specify the format of a number when you display (or parse) it - if you're just looking at the value in the debugger, implicitly casting to string (e.g. setting the .Text property of a control), or calling .ToString without specifying a format, then you're implicitly using the default format of the system (including any cultural specifics). For decimal that default format is G (General), which shows the number in the most compact form possible. So if there is no fractional part to the decimal value, no decimal places are shown.

If you want to specify a particular format when you display the string, then use either string.Format or .ToString

txtValue.Text = amount.ToString("N2");

or use any custom formatting you desire.

Note that some UI controls (perhaps including your TextBox.UIDecimal) have a "Format" property to specify the format that is used when the value is displayed. In that case, you can set the format to N2 in the designer or in code-behind.

D Stanley
  • 149,601
  • 11
  • 178
  • 240