SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Classic ASP cannot subtract!

    Hi,

    <%
    a=17.01
    b=16.50
    response.write a-b & "<br>"
    %>

    The code above should return 0.51
    However it returns 0.510000000000002

    Why is this?

    Thanks
    Simon

  2. #2
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,267
    Mentioned
    116 Post(s)
    Tagged
    1 Thread(s)
    That's because variables aren't strict typed when dealing in classic ASP.

    This will correct the problem 99.999&#37; of the time:

    formatNumber(a-b, 2)
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  3. #3
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that Dave.

    I'm amazed though - I didn't realise that this would mean that it would essentially get simple subtraction wrong!

    Rounding or formating the results - as you suggest will fix this in the majority of cases.

    Simon


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •