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. Web Development
  3. ASP.NET
  4. Division and decimals in C#/ASP.net

Division and decimals in C#/ASP.net

Scheduled Pinned Locked Moved ASP.NET
csharpasp-nethelpquestion
4 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.
  • S Offline
    S Offline
    Stefan R
    wrote on last edited by
    #1

    Hello everyone. I am trying to resize an image by using the relations between its height and width but can't get it to work. Let's say the dimensions are 3*2(width*height), to use this relation in resizing i would have an expression that looks like this: image.height = 100 * (2/3); // (2/3) is eq. to height /width image.widht = 100; this should keep all ratios intact, right? The problem is that the division (width/height) only returns a "0" and not the decimal parts! What am i doing wrong? Will be grateful for all answers / Stefan

    J Y 2 Replies Last reply
    0
    • S Stefan R

      Hello everyone. I am trying to resize an image by using the relations between its height and width but can't get it to work. Let's say the dimensions are 3*2(width*height), to use this relation in resizing i would have an expression that looks like this: image.height = 100 * (2/3); // (2/3) is eq. to height /width image.widht = 100; this should keep all ratios intact, right? The problem is that the division (width/height) only returns a "0" and not the decimal parts! What am i doing wrong? Will be grateful for all answers / Stefan

      J Offline
      J Offline
      Jupiter9
      wrote on last edited by
      #2

      your doing integer arithmetic. Cast to float to retain the decimal and then cast back to int (size type): image.heig;)ht = (int)(100 * (float)2 / (float)3);

      G 1 Reply Last reply
      0
      • J Jupiter9

        your doing integer arithmetic. Cast to float to retain the decimal and then cast back to int (size type): image.heig;)ht = (int)(100 * (float)2 / (float)3);

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

        Cast to double instead of float. The processor does all floating point calculations using the double data type, so if you use float, the values will be converted again before the calculation. The calculation can also be done using integer arithmetic, if the calculations are done in the correct order: image.height = (100 * 2) / 3; (The parantheses can be omitted here, as they don't change the calculation order. I included them for clarity.) --- b { font-weight: normal; }

        1 Reply Last reply
        0
        • S Stefan R

          Hello everyone. I am trying to resize an image by using the relations between its height and width but can't get it to work. Let's say the dimensions are 3*2(width*height), to use this relation in resizing i would have an expression that looks like this: image.height = 100 * (2/3); // (2/3) is eq. to height /width image.widht = 100; this should keep all ratios intact, right? The problem is that the division (width/height) only returns a "0" and not the decimal parts! What am i doing wrong? Will be grateful for all answers / Stefan

          Y Offline
          Y Offline
          Yusubov E
          wrote on last edited by
          #4

          I had also similar situation. What i did was explicitly declaring the type that it number is casting to. Ex: decimal _test = 6 / 4 - will return 1 However decimal _test = (decimal)6 / (decimal)4 will return 1.5

          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