C # decimal formatting text box

I want my money-bound text boxes (SqlServer) to display only two decimal places instead of four places.

I use DevExpress textEdit and CalcEdit with the following display and editing: "#,##0.00#;(#,##0.00#)"; But I always get four decimal places (zeros).

I use the same format string in Janus Grid, the values ​​are displayed correctly.

Any idea, thanks.

+6
source share
4 answers

I highly recommend using inline formatted strings where possible, such as N2 .

 string s = number.ToString("N2"); 

Use John Sheehan . Quick reference information on NET strings . Crib if in doubt. It contains all the necessary information about formatting numbers and dates in .NET.

DevExpress controls typically require you to specify what you are formatting (e.g. number, date, etc.):

 calcEdit.Properties.DisplayFormat.FormatType = FormatType.Numeric; calcEdit.Properties.DisplayFormat.FormatString = "N2"; 

If no format type is specified, the format string will not be applied.

With TextEdit, you must specify a mask instead.

 textEdit.Properties.Mask.MaskType = MaskType.Numeric; textEdit.Properties.Mask.EditMask = "N2"; 
+5
source

Use this:

for example, you have a variable called amount. then format it like this

 amount.ToString("###,###.00"); 

This will give you a sum of two decimal places.

Hope this helps.

+4
source

Try #,##0.00 - you don’t need the final "#".

+3
source

Usually I just write the new formatted value back to the text box after checking it. Therefore, if its decimal is up to thousandths, and then in the event handler being checked, I return the value back to the text after it has been parsed into a float, like this. this.textbox1.Text = speed.ToString ("N3").

0
source

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


All Articles