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. Math.Round..What am I missing?

Math.Round..What am I missing?

Scheduled Pinned Locked Moved C#
questioncsharphelp
10 Posts 6 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.
  • J Offline
    J Offline
    jayart
    wrote on last edited by
    #1

    I need to programatically convert float values like 17.1054688 to 17.11 in C# i.e want to restrict the float values upto 2 decimals. For this I have written following code: private void Roundupto2Decimals(ref float fVal) { string strVal = fVal.ToString("F2"); fVal = float.Parse(strVal); double dVal = (double)fVal; } In VS2005 watch window, I get the correct value for fVal. for e.g: Input : 17.1054688 strVal: "17.11" fVal : 17.11 but ---> (double)fVal : 17.110000610351563 I am not sure when I typecast the same float to double I get unepcted value. I expect dVal to be 17.11 too. Can any one help me understand what is happening? Thanks and Regards, Arti Gujare

    S B L D 4 Replies Last reply
    0
    • J jayart

      I need to programatically convert float values like 17.1054688 to 17.11 in C# i.e want to restrict the float values upto 2 decimals. For this I have written following code: private void Roundupto2Decimals(ref float fVal) { string strVal = fVal.ToString("F2"); fVal = float.Parse(strVal); double dVal = (double)fVal; } In VS2005 watch window, I get the correct value for fVal. for e.g: Input : 17.1054688 strVal: "17.11" fVal : 17.11 but ---> (double)fVal : 17.110000610351563 I am not sure when I typecast the same float to double I get unepcted value. I expect dVal to be 17.11 too. Can any one help me understand what is happening? Thanks and Regards, Arti Gujare

      S Offline
      S Offline
      Stevo Z
      wrote on last edited by
      #2

      Hi, I think it's because C# float is not exact. Try to use decimal instead.

      zilo

      1 Reply Last reply
      0
      • J jayart

        I need to programatically convert float values like 17.1054688 to 17.11 in C# i.e want to restrict the float values upto 2 decimals. For this I have written following code: private void Roundupto2Decimals(ref float fVal) { string strVal = fVal.ToString("F2"); fVal = float.Parse(strVal); double dVal = (double)fVal; } In VS2005 watch window, I get the correct value for fVal. for e.g: Input : 17.1054688 strVal: "17.11" fVal : 17.11 but ---> (double)fVal : 17.110000610351563 I am not sure when I typecast the same float to double I get unepcted value. I expect dVal to be 17.11 too. Can any one help me understand what is happening? Thanks and Regards, Arti Gujare

        B Offline
        B Offline
        benjymous
        wrote on last edited by
        #3

        It's all due to the way floating point numbers are represented - some numbers can't be represented exactly, so will have very tiny errors. As suggested by the other poster, if you are using small numbers, and require high precision, then the Decimal type would be more suitable.

        -- Help me! I'm turning into a grapefruit! Buzzwords!

        J 1 Reply Last reply
        0
        • B benjymous

          It's all due to the way floating point numbers are represented - some numbers can't be represented exactly, so will have very tiny errors. As suggested by the other poster, if you are using small numbers, and require high precision, then the Decimal type would be more suitable.

          -- Help me! I'm turning into a grapefruit! Buzzwords!

          J Offline
          J Offline
          jayart
          wrote on last edited by
          #4

          I tried with decimal too. Did not help much.. This is what I tried. private void Roundupto2Decimals(ref float fVal) { decimal decVal = Math.Round((decimal)fVal, 2); fVal = (float)decVal; double dVal = (double)fVal; } Any other idea?

          S 1 Reply Last reply
          0
          • J jayart

            I need to programatically convert float values like 17.1054688 to 17.11 in C# i.e want to restrict the float values upto 2 decimals. For this I have written following code: private void Roundupto2Decimals(ref float fVal) { string strVal = fVal.ToString("F2"); fVal = float.Parse(strVal); double dVal = (double)fVal; } In VS2005 watch window, I get the correct value for fVal. for e.g: Input : 17.1054688 strVal: "17.11" fVal : 17.11 but ---> (double)fVal : 17.110000610351563 I am not sure when I typecast the same float to double I get unepcted value. I expect dVal to be 17.11 too. Can any one help me understand what is happening? Thanks and Regards, Arti Gujare

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            ArtiGujare wrote:

            Can any one help me understand what is happening?

            http://en.wikipedia.org/wiki/Floating_point[^]

            J 1 Reply Last reply
            0
            • J jayart

              I need to programatically convert float values like 17.1054688 to 17.11 in C# i.e want to restrict the float values upto 2 decimals. For this I have written following code: private void Roundupto2Decimals(ref float fVal) { string strVal = fVal.ToString("F2"); fVal = float.Parse(strVal); double dVal = (double)fVal; } In VS2005 watch window, I get the correct value for fVal. for e.g: Input : 17.1054688 strVal: "17.11" fVal : 17.11 but ---> (double)fVal : 17.110000610351563 I am not sure when I typecast the same float to double I get unepcted value. I expect dVal to be 17.11 too. Can any one help me understand what is happening? Thanks and Regards, Arti Gujare

              D Offline
              D Offline
              dipak dipak
              wrote on last edited by
              #6

              hello, math.round donot support float, its argument must be Double. for example : Double dblValue = 123.126789; dblValue = Math.Round(dblValue, 2); MessageBox.Show(dblValue.ToString());// Displays 123.13 I think u can continue your work. Best of Luck. :-O

              Dipak

              L 1 Reply Last reply
              0
              • J jayart

                I tried with decimal too. Did not help much.. This is what I tried. private void Roundupto2Decimals(ref float fVal) { decimal decVal = Math.Round((decimal)fVal, 2); fVal = (float)decVal; double dVal = (double)fVal; } Any other idea?

                S Offline
                S Offline
                Stevo Z
                wrote on last edited by
                #7

                You are still using float, aren't you ? :) what about this: double dVal = Math.Round((double)fVal, 2); if you need that value exactly, just use decimal in any calculation you are planing. If you want to display exactly 10.11, use decimal. If you're just wondering why is it different, we explained you why and don't worry about it anymore :)

                zilo

                J 1 Reply Last reply
                0
                • L led mike

                  ArtiGujare wrote:

                  Can any one help me understand what is happening?

                  http://en.wikipedia.org/wiki/Floating_point[^]

                  J Offline
                  J Offline
                  jayart
                  wrote on last edited by
                  #8

                  Thanks you Led. The link is quite useful.

                  1 Reply Last reply
                  0
                  • S Stevo Z

                    You are still using float, aren't you ? :) what about this: double dVal = Math.Round((double)fVal, 2); if you need that value exactly, just use decimal in any calculation you are planing. If you want to display exactly 10.11, use decimal. If you're just wondering why is it different, we explained you why and don't worry about it anymore :)

                    zilo

                    J Offline
                    J Offline
                    jayart
                    wrote on last edited by
                    #9

                    The problem is that the third party dll uses float value in its class. So I really cant get rid of the float :-) and I still need to use this dll. As a work around I am using Math.Abs() method to find out tolerable difference between the two float values instead of checking exact difference using "==" with floats.

                    1 Reply Last reply
                    0
                    • D dipak dipak

                      hello, math.round donot support float, its argument must be Double. for example : Double dblValue = 123.126789; dblValue = Math.Round(dblValue, 2); MessageBox.Show(dblValue.ToString());// Displays 123.13 I think u can continue your work. Best of Luck. :-O

                      Dipak

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      Sorry, you're mistaken. Your example happens to produce the result you want, but it is not correct. Here is the background: The way floats/doubles are stored in binary makes it impossible to exactly represent most values, especially the ones that seem like round numbers to humans thinking using base ten. So 4, 2, 1, 0.5, 3.5, 3.75 and many others are representable exactly (basically since they all equal an integer divided by some power of 2); but 3.6 and 3.8 and an infinite number of other values are not. Now whatever float/double function you call, if it returns a float/double will suffer from the same phenomenon. So the only way to really get "3.6" or "3.8" is by using a function that not only rounds but returns a string. I trust there are some formatting methods that do just that in every programming language; In .NET languages ToString() should be one of them, given an appropriate format specifier. If you want to know much more about this topic, here it is: http://docs.sun.com/source/806-3568/ncg\_goldberg.html BTW: to circumvent the floating-point rounding problem, especially for monetary numbers, they introduced the "decimal" data type (which basically stores decimal digits). :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      Sorry for any delays in replying, I currently don't always get e-mail notifications.


                      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