JavaScript window.getComputedStyle [duplicate] - javascript

This question already has answers here:
how to parse string to int in javascript
(9 answers)
Closed 8 years ago.
How can I get windows.getComputed style as an int value?
The way it is displayed by default is string:
var style = window.getComputedStyle(elem1, null);
alert(style.top); //Returns a string

parseInt() will return you the String as an Integer, so your code should look somehow like this:
var style = window.getComputedStyle(elem1, null);
alert(parseInt(style.top));

parseFloat will give you the real value as a number with decimal places preserved:
parseFloat(style.top);

Related

hi, how do i convert a number into a number explicitly [duplicate]

This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Closed 1 year ago.
Convert the string "42" into a number explicitly.
I have to do this in javascript. What's the conversion
that s the problem
Use the parseInt(); function
var a = parseInt("42")
console.log(a);

parseFloat to display with comma [duplicate]

This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Closed 3 years ago.
Is there a way to use the below JavaScript code to use comma's? For example the var num = 1924.00 Is there way to get it to display as 1,924.00?
var num = parseFloat(totalAmount).toFixed(2);
You could use a specific regex like this one in the function below:
function format_currency( value ) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
If you want to use it in conjuction with the .toFixed(), just do like this:
format_currency( 1245.3.toFixed(2) );
Hope it helps.

Why does it show value 5150 and not 155? [duplicate]

This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 4 years ago.
So im quite new to javascript and i tried making something simple as entering a value and add 150 to it but it wont show number + 150?
Pictures below
Code
Output
That's because you are concatenating strings. You need to convert the strings to integers by using parseInt(), and then add the numbers.
https://www.w3schools.com/jsref/jsref_parseint.asp
var fullprice = parseInt(price) + 150;
Because you're actually concatenating a string with a number. The input value is a string, so before operating with it, parse it to int with parseInt
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

transform number with commas in number without commas [duplicate]

This question already has answers here:
Remove commas from the string using JavaScript
(3 answers)
Closed 4 years ago.
How could I transform a number with commas like 123,245 to a number without commas like this 123245, I know that you could already put commas in number without them but how to do that in reverse?
pure JAVASCRIPT please!
var number = Number("123,456".split(",").join(""));
Or:
var number = parseInt("123,456".split(",").join(""));
An alternative is using a regex /,/g
console.log("123,245".replace(/,/g, ''));
console.log("123,245,566".replace(/,/g, ''));

Conver string to integer [duplicate]

This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Closed 8 years ago.
I am getting a numeric value from an element using
$('.myEm').text();
It returns a number from the element, but I need to increment the value by another value.How do I convert it to an integer?
Use parseInt(... ) mate.
var a = parseInt("10")
http://www.w3schools.com/jsref/jsref_parseint.asp

Categories