Whats a simple way to delete the last two characters of a string?
To convert 245px
in 245 just run:
parseInt('245px', 10);
It retains only leading numbers and discards all the rest.
use
var size = parseInt('245px', 10);
where 10 is the radix defining parseInt
is parsing to a decimal value
The parseInt() function parses a string and returns an integer.
The signature is parseInt(string, radix)
The second argument forces parseInt to use a base ten numbering system.
why? if $(this).attr('num') would be "08" parsInt without a radix would become 0
To convert a pixel value without the "px" at the end. use parseFloat.
parseFloat('245px'); // returns 245
Note: If you use parseInt, the value will be correct if the value is an integer. If the value is a decimal one like 245.50px, then the value will be rounded to 245.
This does exactly what you ask: remove last two chars of a string:
s.substr(0, s.length-2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With