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. precision of digits durring conversion

precision of digits durring conversion

Scheduled Pinned Locked Moved C#
csharphelpquestion
4 Posts 4 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.
  • F Offline
    F Offline
    Fred 34
    wrote on last edited by
    #1

    float Width = 47;
    textBox1.Text = System.Convert.ToString(Width);

    Consider above statements. after executing this command the content of textBox1.Text will be 46.99 instead of 47; I've solved this problem by some functions, but I want to see is there any tuning in C# 2010 that I missed it? thanks

    P D P 3 Replies Last reply
    0
    • F Fred 34

      float Width = 47;
      textBox1.Text = System.Convert.ToString(Width);

      Consider above statements. after executing this command the content of textBox1.Text will be 46.99 instead of 47; I've solved this problem by some functions, but I want to see is there any tuning in C# 2010 that I missed it? thanks

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      I'm not sure why you are seeing 46.99. When I run this code, I see 47 written out both times:

      float myVal = 47;
      string text = Convert.ToString(myVal);
      Console.WriteLine(myVal.ToString());
      Console.WriteLine(text);

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      1 Reply Last reply
      0
      • F Fred 34

        float Width = 47;
        textBox1.Text = System.Convert.ToString(Width);

        Consider above statements. after executing this command the content of textBox1.Text will be 46.99 instead of 47; I've solved this problem by some functions, but I want to see is there any tuning in C# 2010 that I missed it? thanks

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        There is no tuning for this. This is a side effect of trying to store an infinite number of floating point numbers in a finite amount of bits. Read this[^] for more information.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        1 Reply Last reply
        0
        • F Fred 34

          float Width = 47;
          textBox1.Text = System.Convert.ToString(Width);

          Consider above statements. after executing this command the content of textBox1.Text will be 46.99 instead of 47; I've solved this problem by some functions, but I want to see is there any tuning in C# 2010 that I missed it? thanks

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4
          1. Avoid float; try Decimal or double 1) Avoid Convert; in this case use ToString, perhaps with a format parameter 2) Avoid TextBox for numeric values; use a NumericUpDown
          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