Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

simX

macrumors 6502a
Original poster
May 28, 2002
765
4
Bay Area, CA
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?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.