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.Floor problem [modified]

Math.Floor problem [modified]

Scheduled Pinned Locked Moved C#
helpquestion
3 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.
  • R Offline
    R Offline
    rbuchana
    wrote on last edited by
    #1

    How come Math.Floor(0.3 / 0.1) returns 2.0??? I assume it is some sort of rounding issue. How can I fix this? Edit: This is what I came up with: Math.Floor(Math.Round(0.3 / 0.1, 2)); Is there an easier way? Thanks

    modified on Tuesday, December 15, 2009 1:03 PM

    K I 2 Replies Last reply
    0
    • R rbuchana

      How come Math.Floor(0.3 / 0.1) returns 2.0??? I assume it is some sort of rounding issue. How can I fix this? Edit: This is what I came up with: Math.Floor(Math.Round(0.3 / 0.1, 2)); Is there an easier way? Thanks

      modified on Tuesday, December 15, 2009 1:03 PM

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      It's because 0.3/0.1 returns a double, and there are all sorts of rounding problems associated with doubles. Console.WriteLine(Math.Floor(0.3M / 0.1M)); uses decimal arithmatic, and you get the expected answer.

      CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

      1 Reply Last reply
      0
      • R rbuchana

        How come Math.Floor(0.3 / 0.1) returns 2.0??? I assume it is some sort of rounding issue. How can I fix this? Edit: This is what I came up with: Math.Floor(Math.Round(0.3 / 0.1, 2)); Is there an easier way? Thanks

        modified on Tuesday, December 15, 2009 1:03 PM

        I Offline
        I Offline
        Ian Shlasko
        wrote on last edited by
        #3

        keefb is 100% correct. If you type "0.3 / 0.1" into the immediate window, it spits out 2.9999999999999996... Now, we all know that's pretty much "3", but if you do a Floor(), it rounds down to 2. As keefb said, use decimals and you'll get the right answer. But if you need to work with doubles, or have other constraints, you could "hack" it by, say, adding 0.0000001 before going through the Floor()... Kind of like the stone-age trick of adding 0.5 before a truncation to simulate rounding up or down to an integer.

        Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)

        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