SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru htown's Avatar
    Join Date
    Mar 2005
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with adding floating points

    If you wouldn’t mind, take a look at the code on THIS PAGE for reference.

    All of the numbers I am dealing with are integers. There are only two that are floating points, (Small Medium Box and Large Box). It seems that these numbers are being rounded down. How can I get them to ad in to all of the other numbers as a floating point and not an integer?

    I am not an expert with JavaScript at all, most of this code was written by a friend of mine a long time ago. I am only tweaking it so a little hand holding may be necessary.

    Thanks,
    Houston
    Houston Brown
    Split Light Designs
    Bringing your ideas to light.

  2. #2
    SitePoint Guru htown's Avatar
    Join Date
    Mar 2005
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I figured it out. I found a parseInt and replaced it with parseFloat and that took care of it.

    Cheers
    Houston Brown
    Split Light Designs
    Bringing your ideas to light.

  3. #3
    Non-Member deathshadow's Avatar
    Join Date
    Jul 2006
    Location
    Dublin, NH
    Posts
    901
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm. By default Javascript uses untyped variables. This means that if you add a real number to a whole number, the resulting variable automatically becomes a real number.

    example:
    var a=1;
    var b=2.5;
    a+=b;

    a would now equal 3.5... There's actually no way to tell a variable to STAY an integer or hold only integers in Javascript. You can convert TO a integer with math.Round (round 0.5 up), math.Ceiling (round ALL fractions up) and math.Floor (throw away all fractions) - but the variable won't stay an integer if you add a real.

    At least, that's how it's supposed to work.

    -- edit -- hah, you posted you fixed it while I was typing my response.


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
  •