简体   繁体   中英

c# textbox decimal formatting

I want my text boxes which are binded to money (SqlServer) entity fields, to show just two decimal places instead of four places.

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

I use the same format string in Janus Grid, the values get display correctly.

Any Idea, thanks.

I would highly recommend using the built-in format strings wherever possible, such as N2 .

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

Use John Sheehan's .NET Format String Quick Reference Cheat Sheet when in doubt. It has all the info you need about formatting numbers and dates in .NET.

DevExpress controls usually require you to also specify what you are formatting (eg a number, date etc.):

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

If the format type isn't specified, the format string won't be applied.

With TextEdit you should specify a Mask instead.

textEdit.Properties.Mask.MaskType = MaskType.Numeric;
textEdit.Properties.Mask.EditMask = "N2";

Use this:

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

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

It will give you amount in two decimal places.

Hope this helps.

尝试#,##0.00 - 你不应该使用尾随的“#”。

I usually just write the new formatted value back into the text box after it has been validated. So if its a decimal to the thousandths then in the validated event handler I write the value back into the text after its been parsed to a float like this. this.textbox1.Text = speed.ToString("N3").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM