SunQuest
 
       ASP.NET Code
  Home arrow ASP.NET Code arrow Page 7 - ASP.NET Basics (part 2): Not My Type
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET CODE

ASP.NET Basics (part 2): Not My Type
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 27
    2003-07-30

    Table of Contents:
  • ASP.NET Basics (part 2): Not My Type
  • Building Blocks
  • A New Assignment
  • Strange Characters
  • Playing The Numbers
  • Operate With Caution
  • Everything Must Go
  • Cast And Credits
  • Mixing It Up
  • It's A Wrap

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    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.

    More ASP.NET Code Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET CODE ARTICLES

    - How to Use the ListBox Control in ASP.NET 2.0
    - How to Load XML Documents in ASP.NET 2.0
    - DataGrid Code
    - ASP.NET Guestbook
    - User Controls and Client Side Scripting
    - ASP.NET Programming with Microsoft's AS...
    - ASP.NET Basics (part 3): Hard Choices
    - ASP.NET Basics (part 2): Not My Type
    - ASP.NET Basics (part 1): Nothing But .Net
    - Directory Tree Browser
    - How to get the confirmation of Yes/No from a...
    - Complete example using custom errors and wri...
    - Paging Certain # records per page .NET style
    - General Methods of formatting and Subtractin...
    - .NET LinkButton web control

    Iron Speed




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway