ASP.NET Basics (part 2): Not My Type - Everything Must Go
(Page 7 of 10 )
In the last few sections, I introduced various numeric data types along with the common arithmetic operations that may be performed on them. As you've seen, this is all fine and dandy so long as you're dealing with data of the same type - but what happens when the day arrives (and it will, make no mistake about that) that you want to mix and match data of different types?
That's a very logical question, and it comes with an interesting answer. In order to respond, I'm going to take you a quick diversion, into a world filled with advertisements for "car finance at a fabulous 0.99 APR" and "mega SALE - 24.66 % discount on all items". In this world, every transaction made undoubtedly has to deal with fractions and integers together.
How does C# handle such a situation? Let's take a look:
<script language="c#" runat="server">
void Page_Load()
{
// price of item
int price = 55;
// discount on item
int discountrate = 24;
// number of items purchased
int numitems = 15;
// discount amount on each item - should equal 13.2
int discountamt = (price * discountrate) / 100;
// grand total for all items - should equal 627
int grandtotal = (price - discountamt) * numitems;
// assign values to appropriate labels
lblprice.Text = "" + price;
lblnumitems.Text = "" + numitems;
lbldiscountamt.Text= "" + discountamt;
lblgrandtotal.Text = "" + grandtotal;
}
</script>
<html>
<head><title>The Mega Sale</title></head>
<body>
<table width="350" cellpadding="5" cellspacing="5" border="3"> <tr><td>Price of selected item:</td><td>$<asp:label id="lblprice" runat="server" />.</td></tr> <tr><td>Number of items purchased:</td><td> <asp:label id="lblnumitems" runat="server" />.</td></tr> <tr><td>Discount for each item:</td><td> $<asp:label id="lbldiscountamt" runat="server" />.</td></tr> <tr><td>Grand total for all items after discount:</td><td> $<asp:label id="lblgrandtotal" runat="server" />.</td></tr> </table> </body> </html>
In the script above, I have defined five variables:
- "price", an integer to store the price of the selected item;
- "discountrate", a integer to store the rate of discount on the item;
- "numitems", a integer to store the number of items purchased;
- "discountamt", a integer to store discount amount per item;
- "grandtotal", an integer to store grand total for all items after discount;
Some basic mathematical calculations and bingo, I arrive at the total amount a customer has to pay after discount. To make things interesting, I have calculated the variable values manually as well; now to check them against the output of the script.

As you can see, there is a discrepancy between the manually-calculated amounts and the output of the script - the discount amount per item should be $13.2, though the output above shows it as $13. Correspondingly, the total amount should be $627, although the script shows it to be $630.
Why did this happen? Simple. In integer arithmetic, all fractional parts of any floating-point number are dropped or rounded off, leading to an incorrect value in the "discountamt" variable in the script above. As you can see, this has a cascading effect on all further calculations, giving rise to a hugely incorrect result (and quite a few angry calls to Customer Services as well).
This problem can be avoided by "casting" the variables appropriately. Casting is the process by which one data type is converted to another data type for the purpose of maintaining the integrity of the calculation. Let's take a closer look.
Next: Cast And Credits >>
More ASP.NET Code Articles
More By Harish Kamath (c) Melonfire