Javascript convert decimal power to regular decimal - javascript

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>

Related

Javascript How to convert a decimal number to a string with specific number of decimal places

In a javascript code, I have a requirement to format a decimal number to a specific number of decimal places and get its exact string representation. For example, If the number is 999999999.9 and the number of decimal places is 8, then the expected value should be "999999999.90000000"
When the Number.toFixed(8) is used it returns a rounded value which is not what I want. Please refer the below code
var num = 999999999.9
var string_rep = num.toFixed(8)
>> the value of string_rep is "999999999.89999998"
I used num.toString() and tried to manually format the decimal part by adding/removing digits, but it does not work for very small numbers like "0.00000008" as the function toString() returns
the scientific notation, i.e. something like "9e-8"
So what should be the proper approach for this?
Number.prototype.toLocaleString will do the trick
num.toLocaleString('en-US', {minimumFractionDigits: 8, useGrouping: false})//"999999999.90000000"

What is number and how to convert?

I have custom calculator with js, everything was normal until i found 1.2101997764095421e-12 sometimes X.XXe-13, X.XXe-14
What is this ? How to convert it ?
Update :
I have knowledge in excel for this calculator, the result for 1.2101997764095421e-12 should be a 14,78
Those are really long floating point number, and the e-12 represents that there are 12 decimals. To convert it to your desired format you can do this:
var converted = Math.round(number * 100) / 100
This is nothing specific to javascript but a common mathematical notation meaning the following:
1e-1 = 0.1
1e-2 = 0.01
x.xxe-13 points to a number having 13 decimals after 0.
This is a representation of Scientific Notation. You can translate the eN to mean x10^N. When a Float number gets to large or small the string representation number will look that way.
1.12345e5 = 1.12345x10^5 = 112345
1.12345e-5 = 1.12345x10^-5 = .0000112345

convert number to two decimal places and return as a number and not string

Given a variable
var str = 1;
convert str to output 1.00 as a number and not string.
so the output should be 1.00
and not "1.00"
what javascript operations should I use to do this?.
str.toFixed(2) returns a string and not a number so please..
When you're talking about numbers, there is no difference between 1 and 1.00. So, if it's a number, Javascript will treat them the same. If you want it as 1.00, the only real way to do that is by creating a string of it with something like:
var nnn = 1;
var sss = nnn.toFixed(2));
The presentation of that number (either as a string or direct to output) may be under your control but the number itself is not (other than changing the value of course but, as already mentioned, there is no difference between the values 1, 1.0 or 1e0).
JavaScript only has one kind of number: "number", which is a IEEE-754 Double Precision
See this question to format to 2 decimal places.

How can I parse a string as an integer and keep decimal places if they are zeros?

I have these strings: "59.50" & "30.00"
What I need to do is convert them to integers but keep the trailing zeros at the end to effectively return:
59.50
30.00
I've tried:
Math.round(59.50 * 1000) / 1000
Math.round(30.00 * 1000) / 1000
but ended up with
59.5
30
I'm assuming I need to use a different method than Math.round as this automatically chops off trailing zeros.
I need to keep these as integers as they need to be multiplied with other integers and keep two decimals points. T thought this would be fairly straight forward but after a lot of searching I can't seem to find a solution to exactly what I need.
Thanks!
Your premise is flawed. If you parse a number, you are converting it to its numerical representation, which by definition doesn't have trailing zeros.
A further flaw is that you seem to think you can multiply two numbers together and keep the same number of decimal places as the original numbers. That barely makes sense.
It sounds like this might be an XY Problem, and what you really want to do is just have two decimal places in your result.
If so, you can use .toFixed() for this:
var num = parseFloat("59.50");
var num2 = parseFloat("12.33");
var num3 = num * num2
console.log(num3.toFixed(2)); // 733.64
Whenever you want to display the value of the variable, use Number.prototype.toFixed(). This function takes one argument: the number of decimal places to keep. It returns a string, so do it right before viewing the value to the user.
console.log((123.4567).toFixed(2)); // logs "123.46" (rounded)
To keep the decimals - multiply the string by 1
example : "33.01" * 1 // equals to 33.01
Seems you are trying to retain the same floating point, so better solution will be some thing like
parseFloat(string).toFixed(string.split('.')[1].length);
If you want numbers with decimal points, you are not talking about integers (which are whole numbers) but floating point numbers.
In Javascript all numbers are represented as floating point numbers.
You don't need the trailing zeros to do calculations. As long as you've got all the significant digits, you're fine.
If you want to output your result with a given number of decimal values, you can use the toFixed method to transform your number into a formatted string:
var num = 1.5
var output = num.toFixed(2) // '1.50'
// the number is rounded
num = 1.234
output = num.toFixed(2) // '1.23'
num = 1.567
output = num.toFixed(2) // '1.57'
Here's a more detailed description of toFixed: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Dealing With Binary / Bitshifts in JavaScript

I am trying to perform some bitshift operations and dealing with binary numbers in JavaScript.
Here's what I'm trying to do. A user inputs a value and I do the following with it:
// Square Input and mod with 65536 to keep it below that value
var squaredInput = (inputVal * inputVal) % 65536;
// Figure out how many bits is the squared input number
var bits = Math.floor(Math.log(squaredInput) / Math.log(2)) + 1;
// Convert that number to a 16-bit number using bitshift.
var squaredShifted = squaredInput >>> (16 - bits);
As long as the number is larger than 46, it works. Once it is less than 46, it does not work.
I know the problem is the in bitshift. Now coming from a C background, I know this would be done differently, since all numbers will be stored in 32-bit format (given it is an int). Does JavaScript do the same (since it vars are not typed)?
If so, is it possible to store a 16-bit number? If not, can I treat it as 32-bits and do the required calculations to assume it is 16-bits?
Note: I am trying to extract the middle 4-bits of the 16-bit value in squaredInput.
Another note: When printing out the var, it just prints out the value without the padding so I couldn't figure it out. Tried using parseInt and toString.
Thanks
Are you looking for this?
function get16bitnumber( inputVal ){
return ("0000000000000000"+(inputVal * inputVal).toString(2)).substr(-16);
}
This function returns last 16 bits of (inputVal*inputVal) value.By having binary string you could work with any range of bits.
Don't use bitshifting in JS if you don't absolutely have to. The specs mention at least four number formats
IEEE 754
Int32
UInt32
UInt16
It's really confusing to know which is used when.
For example, ~ applies a bitwise inversion while converting to Int32. UInt16 seems to be used only in String.fromCharCode. Using bitshift operators converts the operands to either UInt32 or to Int32.
In your case, the right shift operator >>> forces conversion to UInt32.
When you type
a >>> b
this is what you get:
ToUInt32(a) >>> (ToUInt32(b) & 0x1f)

Categories

Resources