...

View Full Version : Scientific Notation to standard decimal

Aboxofdonuts
05-24-2012, 11:40 PM
I'm using java to convert a number that was input by the user in a text box. The user then selects the conversion to do, and then it outputs the converted number to another text box.

However, the conversion equations use scientific notation. I want to make another text box that takes the result in scientific notation and turns it into standard decimal format.

example:
x=1
equation is x*1e-6
first result text box would read
1e-6
second result text box would read
1000000

I've never had to do something like this before, so I'm clueless as to where to even begin. Google proved kinda useless. Couple of ideas but I didn't understand them or couldn't get them to work.

Any ideas? Not sure if this will be simple or not lol.
Thanks in advanced :D

xelawho
05-24-2012, 11:58 PM
mmm... but doesn't 1e-6 equal 0.000001?

javascript has built-in methods for converting to and from exponential numbers:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number

so you can do something like this:

<html>
<head>
</head>
<body>
<input type="text" value ="1e-6" id="start">
<input type="button" value="convert" onclick="convert()">
<input type="text" id="end">
<script type='text/javascript'>

function convert(){
document.getElementById("end").value=Number(document.getElementById("start").value).toPrecision()
}
</script>
</body>

</html>

Aboxofdonuts
05-25-2012, 12:42 AM
mmm... but doesn't 1e-6 equal 0.000001?

My bad, in the original post I did 1e6 instead of 1e-6.

I was trying the code you used in a brand new dreamweaver file, and if you change the text box to 1e-7 the second text box comes out as scientific notation. Is there a limit to what it can convert? 1e7 works, 1e20 works, but it seems negatives don't.

Some of the equations are like this: 2.47105381e-16 . So I'd hate to have negative numbers not work.

Edit: Just tested my project again, and it seems to automatically convert to decimal when it does the math, however, it only works till a certain amount like stated above.

Now, I'm just scratching my head on this lol.

Old Pedant
05-25-2012, 01:47 AM
No, toPrecision() does *NOT* force scientific notation, at all. Dunno why Xelawho gave you that answer.

Actually, if you use toPrecision() [no argument] it is no different than toString(). Only if you give it an argument will it be different. But it still won't FORCE scientific notation.

You want toExponential(digits)

Example:

<script type="text/javascript">
var exp = -30;
while ( exp < 31 )
{
var n = Number("1e" + exp);
document.write( n.toExponential(8) + "<br/>" );
exp += 2;
}
</script>

Philip M
05-25-2012, 08:45 AM
I'm using java to convert a number

This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

See: http://www.codingforums.com/showthread.php?t=138859

You convert a positive or negative scientific format number to standard decimal simply with *1. But 2.47105381e-16 is too big a number for Javascript to handle.

<input type = "text" id = "txt1" value = 1e06>
<input type = "text" id = "txt2">
<input type = button value = "Click" onclick = "makeNumber()">

<script type="text/javascript">

function makeNumber() {
var x = document.getElementById("txt1").value;
document.getElementById("txt2").value = x*1;

}
</script>

Further examples to study:-

<script type="text/javascript">

alert (Math.pow(10,20);
alert (Math.pow(10,21)); // Javascript can display numbers up to 10 ^ 20, thereafter in scientific/exponential notation.

alert (parseFloat(1.51e-6)); // returns 0.00000151
alert (parseFloat(1.23e-7)); // returns 1.23e-7

// You can use toFixed() instead of parseFloat() to format positive numbers the way you want up to 20 places of decimals.
// For example (1.23e-7).toFixed(9) will render as 0.000000123

alert (2.47105381e-16.toFixed(20)); // returns positive value
alert ((-2.47105381e-16).toFixed(20)); // using parentheses returns negative value
alert (2.47105381e-16.toFixed(21)); // out of range

// Using .toFixed() with negative numbers requires the value to be placed in parentheses as, due to operator precedence, negative numbers don't return a string.

alert (-1.23e-2.toFixed(9)); // -0.0123
alert (-1.23e-4.toFixed(9)); // -0.000123
alert ((-1.23e-4).toFixed(9)); // -0.000123000

</script>

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum