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 / C++ / MFC
  4. Cut Digits from 0.1000000000001

Cut Digits from 0.1000000000001

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 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.
  • A Offline
    A Offline
    AnTri
    wrote on last edited by
    #1

    Hey, how can I cut needless decimal digits from a calculation result e.g. calculation with pow(10.0,-1.0) 10^-1 = 0.100000000001 but I need to have 0.1

    C C 2 Replies Last reply
    0
    • A AnTri

      Hey, how can I cut needless decimal digits from a calculation result e.g. calculation with pow(10.0,-1.0) 10^-1 = 0.100000000001 but I need to have 0.1

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      You can't. This is due to the precision of the floating point representation. But why do you want to do such a thing ? I think the error due to precision is very neglectable here.


      Cédric Moonen Software developer
      Charting control [Updated - v1.1]

      1 Reply Last reply
      0
      • A AnTri

        Hey, how can I cut needless decimal digits from a calculation result e.g. calculation with pow(10.0,-1.0) 10^-1 = 0.100000000001 but I need to have 0.1

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        You mean for display, or internally ? ((int)(x*10))/10.0 would give you one decimal place. Not sure if there's a more built in way to do it.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        A 1 Reply Last reply
        0
        • C Christian Graus

          You mean for display, or internally ? ((int)(x*10))/10.0 would give you one decimal place. Not sure if there's a more built in way to do it.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          A Offline
          A Offline
          AnTri
          wrote on last edited by
          #4

          I need this for displaying the result value (e.g. in a diagram)

          D 1 Reply Last reply
          0
          • A AnTri

            I need this for displaying the result value (e.g. in a diagram)

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            So just format the value using sprintf(..., "%.1f", ...).


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            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