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. C# Denies simple math? :\

C# Denies simple math? :\

Scheduled Pinned Locked Moved C#
csharpquestionannouncement
5 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.
  • S Offline
    S Offline
    sharpiesharpie
    wrote on last edited by
    #1

    I've added a progress bar to my app and i'm trying to update it but the result is only 0 and then 100. int t = int.Parse(textBox1.Text); double s = ((p/t)*100); progressBar1.Value = (int)s; t (textBox1) is how many times to do the operation. p is the counter for how many times it has been done. ive followed the program with breakpoints and s is always 0.0, even when p is 10 and t is 100 ((10/100)*100) = 10. wtf? :\

    C G 2 Replies Last reply
    0
    • S sharpiesharpie

      I've added a progress bar to my app and i'm trying to update it but the result is only 0 and then 100. int t = int.Parse(textBox1.Text); double s = ((p/t)*100); progressBar1.Value = (int)s; t (textBox1) is how many times to do the operation. p is the counter for how many times it has been done. ive followed the program with breakpoints and s is always 0.0, even when p is 10 and t is 100 ((10/100)*100) = 10. wtf? :\

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

      Try this: double s = p * 100 / t; It doesn't matter what order you do divide and multiply on paper, you get the same result. However, if you divide by an int, you get an int and if it's < 1, it will round to an int, so the * 100 isn't doing what you'd hoped. double t = double.Parse(textBox1.Text); double s = ((p/t)*100); or int t = int.Parse(textBox1.Text); double s = ((p/(double)t)*100); would also work.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      1 Reply Last reply
      0
      • S sharpiesharpie

        I've added a progress bar to my app and i'm trying to update it but the result is only 0 and then 100. int t = int.Parse(textBox1.Text); double s = ((p/t)*100); progressBar1.Value = (int)s; t (textBox1) is how many times to do the operation. p is the counter for how many times it has been done. ive followed the program with breakpoints and s is always 0.0, even when p is 10 and t is 100 ((10/100)*100) = 10. wtf? :\

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        I believe that you are a former VB programmer? In VB the / operator is only a floating point operator, and the integer division operator is \. In C# the / operator is defined for both data types, so which one is used depends on the data you use it on. The exact equivalent of p/t in VB would in C# be (double)p/(double(t), as VB would automatically convert the values to accommodate the operator.

        --- single minded; short sighted; long gone;

        C 1 Reply Last reply
        0
        • G Guffa

          I believe that you are a former VB programmer? In VB the / operator is only a floating point operator, and the integer division operator is \. In C# the / operator is defined for both data types, so which one is used depends on the data you use it on. The exact equivalent of p/t in VB would in C# be (double)p/(double(t), as VB would automatically convert the values to accommodate the operator.

          --- single minded; short sighted; long gone;

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

          Is there any reason to cast p to double ? I thought casting t to double is all that's needed here.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          G 1 Reply Last reply
          0
          • C Christian Graus

            Is there any reason to cast p to double ? I thought casting t to double is all that's needed here.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Yes, casting only one of the operands is the minimum required to make it work. That would decide what operator to use, so the other operator will also be cast to the same type. Whether it's done implicitly or explicitly, the generated code will be the same. I prefer doing explicit casting in situations like this. It shows the intention of the code more clearly than if one relies on implicit conversions.

            --- single minded; short sighted; long gone;

            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