g.DrawString to display floating point?
-
I want to display a float on a WinForm, but this only gives an int. Why? SomeFunc(graphics g) { ... float x = 0.0f; Font theFont(....); //more stuff... .... //->HERE!! g.DrawString(x.ToString(),... } Why do I get "0" and not "0.0"? thanks in advance....
-
I want to display a float on a WinForm, but this only gives an int. Why? SomeFunc(graphics g) { ... float x = 0.0f; Font theFont(....); //more stuff... .... //->HERE!! g.DrawString(x.ToString(),... } Why do I get "0" and not "0.0"? thanks in advance....
You did not specify a format string with the ToString() method, so the default formatting in the current culture with minimum number of characters is used. Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke
-
I want to display a float on a WinForm, but this only gives an int. Why? SomeFunc(graphics g) { ... float x = 0.0f; Font theFont(....); //more stuff... .... //->HERE!! g.DrawString(x.ToString(),... } Why do I get "0" and not "0.0"? thanks in advance....
You must specify the format by calling the System.Single.ToString(string format) overload.
Standard numeric formats are listed on MSDN at Standard Numeric Format Strings.
Custom numeric formatting is discussed at Custom Numeric Format Strings. -
You did not specify a format string with the ToString() method, so the default formatting in the current culture with minimum number of characters is used. Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke
Rob, Thanks for your response. I must admit I'm a bit lost.. Here's my code: //////////////////////////////////////////////////////////////// //Label x axis. Font graphFont = new Font("Arial",8); Brush graphBrush = new SolidBrush(Color.BlueViolet); float x = 0.0f; String xString = x.ToString(); for(float gx = xIndent; gx <= graphRect.Width; gx += gx) { g.DrawString(xString, graphFont, graphBrush, this.ClientRectangle.Left + xIndent, yOrigin + 4); } ////////////////////////////////////////////////////////////////// What will give me "0.0" instead of "0"? -thanks again
-
Rob, Thanks for your response. I must admit I'm a bit lost.. Here's my code: //////////////////////////////////////////////////////////////// //Label x axis. Font graphFont = new Font("Arial",8); Brush graphBrush = new SolidBrush(Color.BlueViolet); float x = 0.0f; String xString = x.ToString(); for(float gx = xIndent; gx <= graphRect.Width; gx += gx) { g.DrawString(xString, graphFont, graphBrush, this.ClientRectangle.Left + xIndent, yOrigin + 4); } ////////////////////////////////////////////////////////////////// What will give me "0.0" instead of "0"? -thanks again
String xString = x.ToString("F1"); //format as floating point with 1 digit to right of decimal point using the current culture. Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke