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. real simple question - decimals

real simple question - decimals

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
7 Posts 5 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.
  • S Offline
    S Offline
    Sam 2006
    wrote on last edited by
    #1

    hey all, i have a really simple question, im amazed i cant find out how to do this. i have 2 integers, called x and y. i need to show printf("%d", (x/y)); everything i try will round it to the closest integer. i've tried using double for x,y, another variable that stores the value of x/y, but it still rounds the to the closest integer. how can i display a number not rounded to the closest integer? thanks in advance

    M B H 3 Replies Last reply
    0
    • S Sam 2006

      hey all, i have a really simple question, im amazed i cant find out how to do this. i have 2 integers, called x and y. i need to show printf("%d", (x/y)); everything i try will round it to the closest integer. i've tried using double for x,y, another variable that stores the value of x/y, but it still rounds the to the closest integer. how can i display a number not rounded to the closest integer? thanks in advance

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      You also need to change %d to %lf to tell printf() that it should output a double, not an int.

      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

      B 1 Reply Last reply
      0
      • S Sam 2006

        hey all, i have a really simple question, im amazed i cant find out how to do this. i have 2 integers, called x and y. i need to show printf("%d", (x/y)); everything i try will round it to the closest integer. i've tried using double for x,y, another variable that stores the value of x/y, but it still rounds the to the closest integer. how can i display a number not rounded to the closest integer? thanks in advance

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

        Don't forget to make sure at least one of the components in the expression is a float or a double to avoid truncations. If both x and y are int's you will notice a truncation of the result before the printf formatting occurs. printf("%f\n",(double)x/y);

        1 Reply Last reply
        0
        • S Sam 2006

          hey all, i have a really simple question, im amazed i cant find out how to do this. i have 2 integers, called x and y. i need to show printf("%d", (x/y)); everything i try will round it to the closest integer. i've tried using double for x,y, another variable that stores the value of x/y, but it still rounds the to the closest integer. how can i display a number not rounded to the closest integer? thanks in advance

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          See double c; double x = 123.456; double y = 2.1; c=x/y; printf( "%f\n", c);//output->58.788571_**


          **_

          whitesky


          S 1 Reply Last reply
          0
          • M Michael Dunn

            You also need to change %d to %lf to tell printf() that it should output a double, not an int.

            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

            B Offline
            B Offline
            bob16972
            wrote on last edited by
            #5

            If both of the numbers are of type int like they stated then you'll get (On Visual C++ 6.0 and VC++ 2003) #include "stdafx.h" int main(int argc, char* argv[]) { int x=1; int y=9; printf("%f\n",x/y); // The expression gets truncated before formatting printf("%lf\n",x/y); // The expression gets truncated before formatting printf("%f\n",(double)x/y); // The correct way when both are integers printf("%lf\n",(double)x/y); // The correct way when both are integers return 0; } *************** OUTPUT VC++ 6.0******************* -1.#QNAN0 -1.#QNAN0 0.111111 0.111111 Press any key to continue *************** OUTPUT VC++ 6.0******************* 796482944349676280000000000000000000000000000000000 000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000 000000000000000000000000000000.000000 796482944349676280000000000000000000000000000000000 000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000 000000000000000000000000000000.000000 0.111111 0.111111 Press any key to continue

            1 Reply Last reply
            0
            • H Hamid Taebi

              See double c; double x = 123.456; double y = 2.1; c=x/y; printf( "%f\n", c);//output->58.788571_**


              **_

              whitesky


              S Offline
              S Offline
              Sam 2006
              wrote on last edited by
              #6

              that worked thanks!

              G 1 Reply Last reply
              0
              • S Sam 2006

                that worked thanks!

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

                One slight correction: Use the "%f" format code for values of type float, and "%lf" format code for values of type double.


                Software Zen: delete this;

                Fold With Us![^]

                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