Using Javascript Math I can convert a decimal number like 100 into decimal power number like 1e+2
Math.pow(10, 2)
But how can I do it the other way round? How can I convert a decimal power like 1e+2 to a regular decimal like 100? I need this to save a number in a database number column. I could not find any method or jquery plugin that will do this for me.
The expression you showed above, Math.pow(10, 2)
, actually evaluates to 100
.
But if you are given the string 1e+2
, you can change it into 100
by calling parseFloat()
:
var s = '1e+2', // This is a string in scientific notation. x = parseFloat(s); // Here we convert it to the more widespread notation. document.write('"'+s+'" -> '+x); // Let's print out the values for testing purposes.
What you are talking about is number formats, not numbers.
The string "100"
is the textual representation of the number 100
. The string "1e+2"
is the textual representation of the same number, but in scientific format.
A numeric value doesn't have any specific format. The values in the variables a
, b
and c
in this code are completely identical:
var a = 100;
var b = Math.pow(10, 2);
var c = 1e+2;
A numeric value only gets a specific format when you create a textual represenation of the number. I this code the variables from the previous example are displayed as text:
console.log(a);
console.log(b);
console.log(c);
As the values are not to small or too large to be resonably represented in the regular decimal format, that's how they come out:
100
100
100
When you want to store a numeric value in the database, it doesn't matter where the number came from, it's just the numeric value itself that is used.
Math.pow(x, y)
actually returns the result of x^y. In your specific case, it is being represented in scientific notation, but the actual number is still being 100. If you have a string and want to get the actual number, you can use:
Number("1e+2")
Example
$("#result").append(Number("1e+2"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="result"></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.