Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. g.DrawString to display floating point?

g.DrawString to display floating point?

Scheduled Pinned Locked Moved C#
graphicsquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • . Offline
    . Offline
    ...---...
    wrote on last edited by
    #1

    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....

    R S 2 Replies Last reply
    0
    • . ...---...

      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....

      R Offline
      R Offline
      Rob Graham
      wrote on last edited by
      #2

      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

      . 1 Reply Last reply
      0
      • . ...---...

        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....

        S Offline
        S Offline
        Sebrell
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • R Rob Graham

          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

          . Offline
          . Offline
          ...---...
          wrote on last edited by
          #4

          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

          R 1 Reply Last reply
          0
          • . ...---...

            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

            R Offline
            R Offline
            Rob Graham
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups