Division and decimals in C#/ASP.net
-
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
-
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
-
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);
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; }
-
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