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. convert float to int without rounding

convert float to int without rounding

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
5 Posts 4 Posters 1 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.
  • D Offline
    D Offline
    Dev578
    wrote on last edited by
    #1

    How would one convert a float to an int without rounding it? Currently: float f = 1.9; int i = (int)f; i would equal 2. How do I get it to not round, and have i equal 1 in the above example? Any help is appreciated. -Dev578

    T PJ ArendsP G 3 Replies Last reply
    0
    • D Dev578

      How would one convert a float to an int without rounding it? Currently: float f = 1.9; int i = (int)f; i would equal 2. How do I get it to not round, and have i equal 1 in the above example? Any help is appreciated. -Dev578

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      As defined by the C++ standard, floating to integer conversions truncate. So 1.9 would convert to 1. Tim Smith I'm going to patent thought. I have yet to see any prior art.

      1 Reply Last reply
      0
      • D Dev578

        How would one convert a float to an int without rounding it? Currently: float f = 1.9; int i = (int)f; i would equal 2. How do I get it to not round, and have i equal 1 in the above example? Any help is appreciated. -Dev578

        PJ ArendsP Offline
        PJ ArendsP Offline
        PJ Arends
        wrote on last edited by
        #3

        int i = (int)(f + 0.5);


        [

        ](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!

        Within you lies the power for good; Use it!

        G 1 Reply Last reply
        0
        • PJ ArendsP PJ Arends

          int i = (int)(f + 0.5);


          [

          ](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          You expression rounds, which I think is the opposite of what he wants, which is simple truncation.


          Software Zen: delete this;

          1 Reply Last reply
          0
          • D Dev578

            How would one convert a float to an int without rounding it? Currently: float f = 1.9; int i = (int)f; i would equal 2. How do I get it to not round, and have i equal 1 in the above example? Any help is appreciated. -Dev578

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #5

            i would not equal 2. Simple 'C'-style casts truncate, so you will see i == 1.


            Software Zen: delete this;

            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