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. Help with calculating percentage in C

Help with calculating percentage in C

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 5 Posters 6 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hope no one screams at me but if they do I guess its okay this question is simple its ridiculous but here goes my total number in decimal is 1,621,270,208 the number I am trying to figure out the percent is from is 1,604,667,016 I did this calculation on the web and it came out to 98% the two number the first being total is a int the second unalloc is also a int. the variable percent is a double or float I thought mistaking that percent = unalloc / total woudgive me 0000.98 in percent but I realize that since the quotient is zero percent is 000.000 doing percent = unalloc % total would give the reminder in percent and thus a value of 1604665335.000 I have searched the web I know this is real simple to some of you wonder if you could help thanks in advance

    K V 2 Replies Last reply
    0
    • F ForNow

      Hope no one screams at me but if they do I guess its okay this question is simple its ridiculous but here goes my total number in decimal is 1,621,270,208 the number I am trying to figure out the percent is from is 1,604,667,016 I did this calculation on the web and it came out to 98% the two number the first being total is a int the second unalloc is also a int. the variable percent is a double or float I thought mistaking that percent = unalloc / total woudgive me 0000.98 in percent but I realize that since the quotient is zero percent is 000.000 doing percent = unalloc % total would give the reminder in percent and thus a value of 1604665335.000 I have searched the web I know this is real simple to some of you wonder if you could help thanks in advance

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      Presumably you're doing something like

      int total = 100;
      int unalloc = 96;
      double percent = unalloc/total;

      So the problem here is that since unaloc and total are both int, the calculation is done in integer math, and then promoted to double. What you need to do is to promote either operand to the division to double (or both):

      double percent = (double)unalloc/total:

      Keep Calm and Carry On

      F C 2 Replies Last reply
      0
      • K k5054

        Presumably you're doing something like

        int total = 100;
        int unalloc = 96;
        double percent = unalloc/total;

        So the problem here is that since unaloc and total are both int, the calculation is done in integer math, and then promoted to double. What you need to do is to promote either operand to the division to double (or both):

        double percent = (double)unalloc/total:

        Keep Calm and Carry On

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        that was it thank you so so so much

        1 Reply Last reply
        0
        • F ForNow

          Hope no one screams at me but if they do I guess its okay this question is simple its ridiculous but here goes my total number in decimal is 1,621,270,208 the number I am trying to figure out the percent is from is 1,604,667,016 I did this calculation on the web and it came out to 98% the two number the first being total is a int the second unalloc is also a int. the variable percent is a double or float I thought mistaking that percent = unalloc / total woudgive me 0000.98 in percent but I realize that since the quotient is zero percent is 000.000 doing percent = unalloc % total would give the reminder in percent and thus a value of 1604665335.000 I have searched the web I know this is real simple to some of you wonder if you could help thanks in advance

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #4

          double total = 1621270208;
          double percentFrom = 1604667016;
          double percent = percentFrom / total * 100;

          F 1 Reply Last reply
          0
          • K k5054

            Presumably you're doing something like

            int total = 100;
            int unalloc = 96;
            double percent = unalloc/total;

            So the problem here is that since unaloc and total are both int, the calculation is done in integer math, and then promoted to double. What you need to do is to promote either operand to the division to double (or both):

            double percent = (double)unalloc/total:

            Keep Calm and Carry On

            C Offline
            C Offline
            Calin Negru
            wrote on last edited by
            #5

            hey k5054 my comment does not relate to the currently running thread. I just wanted to let you know I keep getting back to some of your replies in topic I have posted quite a while ago. I thought it might be a good idea to let you know I`m haunting those posts.

            R 1 Reply Last reply
            0
            • V Victor Nijegorodov

              double total = 1621270208;
              double percentFrom = 1604667016;
              double percent = percentFrom / total * 100;

              F Offline
              F Offline
              ForNow
              wrote on last edited by
              #6

              Thanks

              1 Reply Last reply
              0
              • C Calin Negru

                hey k5054 my comment does not relate to the currently running thread. I just wanted to let you know I keep getting back to some of your replies in topic I have posted quite a while ago. I thought it might be a good idea to let you know I`m haunting those posts.

                R Offline
                R Offline
                RedDk
                wrote on last edited by
                #7

                Hey lookee here ... a joke icon! That's what I needed the other day. Oh well. :thumbsup:

                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