PDA

View Full Version : Small JavaScript Question


simX
Aug 13, 2003, 01:38 PM
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
Aug 13, 2003, 03:54 PM
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.

simX
Aug 13, 2003, 04:31 PM
Hmm, I thought I tried that and it didn't work... but it actually did. Thanks! :)