Small JavaScript Question

Discussion in 'General Mac Discussion' started by simX, Aug 13, 2003.

  1. macrumors 6502a

    simX

    Joined:
    May 28, 2002
    Location:
    Bay Area, CA
    #1
    I've been doing some tiny JavaScript stuff for web pages, and I've run across a problem. I want to convert some large numbers into exponential notation.

    It seems the method is varname.toExponential(a), where a inside is the number of decimal places to which you want the number rounded.

    The problem is, this method seems to not work with Safari, so I was wondering what the best way is to check if a browser supports this method, and if it doesn't, then just display the big long number. I don't want to simply browser sniff for Safari, because other browsers (like Mac IE) don't support it either -- so it would be nice just to check if the browser supports the function, and then use it if it does.

    Any suggestions?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    I think this is what you want:
    <HTML>
    <HEAD>
    <TITLE>Test</TITLE>
    </HEAD>
    <BODY>
    <SCRIPT>
    var longNum=0.987654321987654321;
    alert (longNum);
    if (longNum.toExponential)
    {
    alert(longNum.toExponential(5));
    }
    </SCRIPT>
    </BODY>
    </HTML>

    On Safari (1.1) I only see the first alert.
     
  3. thread starter macrumors 6502a

    simX

    Joined:
    May 28, 2002
    Location:
    Bay Area, CA
    #3
    Hmm, I thought I tried that and it didn't work... but it actually did. Thanks! :)
     

Share This Page