Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying in scientific notation 2

Status
Not open for further replies.

anpfire

Programmer
Oct 8, 2003
22
CA
Hi,
I am displaying a table in a webpage that gets its information dynamically from an Access database. The numbers that it gets from the database have values till the 20th decimal place or more (like 0.00000834762532893428).
I want to display this number in scientific notation with approximately 3-4 decimal places.
Example:
0.00000834762532893428 --> 8.3476E-6

If you have any sample javascript function that will allow me to do this, I'd greatly appreciate it. Thanks.
Anoop.
 
I googled and found this, it might need a hair of revision but it'll give you something to start with:
Code:
<HTML>
<HEAD>
<TITLE>Test</TITLE>
<SCRIPT LANGUAGE=JavaScript>
function toSci(s, eng){
    var exp = 0;
    var negative = false;
    if (s.length > 0 && s.charAt(0) == '-'){
        negative = true;
        s = s.substring(1,s.length);
    }
    var splitter = s.split(new RegExp('[eE]'));
    if (splitter.length > 1){
        exp = parseInt(splitter[1]);
        s = splitter[0];
    }
    splitter = s.split(/[\.]/);
    if (splitter.length > 1){
        s = splitter[0] + splitter[1];
        exp += splitter[0].length -1;
    } else {
        exp += s.length - 1;
    }      
    var leadingZeros = 0;
    for (leadingZeros=0; leadingZeros < s.length  && s.charAt(leadingZeros) == '0'; leadingZeros++){
       exp = exp - 1;
    } 
    s = s.substring(leadingZeros, s.length);
    var moveDec;
    if (eng){
        if (exp>=0){
            moveDec = (exp % 3) + 1;
        } else {            
            moveDec = 4 - ((-exp) % 3);
            if (moveDec == 4){
                moveDec = 1;
            }
        }  
        exp -= (moveDec - 1);            
    } else {
        moveDec = 1;
    }
    var trailingZeros = '';
    for (var i=s.length; i<moveDec; i++){
        trailingZeros+='0';
    }
    return (
        (negative?'-':'') +
        ((s.length==0)?'0':s.substring(0,moveDec)) + 
        ((s.length<=moveDec)?trailingZeros:('.'+s.substring(moveDec,s.length))) +
        ((s.length==0||exp==0)?'':('e'+exp))
    );
}
</SCRIPT>
<BODY>
<FORM NAME=&quot;blahForm&quot;>
<input type=text name=blahText>
<input type=button name=blahButton value='Show Scientific' onclick='alert(toSci(blahForm.blahText.value, false));'>
</FORM>
</BODY>
</HTML>

-kaht

banghead.gif
 
var n = 0.00000834762532893428;
alert( n.toExponential(4) );



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Thankyou Jeff. That was exactly what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top