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 for开发者_开发知识库mat 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 (e.g. 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.
Try #,##0.00
-- you shouldn't need the trailing "#".
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").
精彩评论