Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete "px" from 245px

Whats a simple way to delete the last two characters of a string?

like image 995
Tomkay Avatar asked Feb 01 '11 08:02

Tomkay


4 Answers

To convert 245px in 245 just run:

parseInt('245px', 10);

It retains only leading numbers and discards all the rest.

like image 160
Don Avatar answered Nov 15 '22 15:11

Don


use

var size = parseInt('245px', 10);

where 10 is the radix defining parseInt is parsing to a decimal value

use parseInt but don't use parseInt without a radix

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.

  • The default input type for ParseInt() is decimal (base 10).
  • If the number begins in "0", it is assumed to be octal (base 8).
  • If it begins in "0x", it is assumed to be hexadecimal

why? if $(this).attr('num') would be "08" parsInt without a radix would become 0

like image 32
Caspar Kleijne Avatar answered Nov 15 '22 16:11

Caspar Kleijne


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.

like image 24
Foreever Avatar answered Nov 15 '22 16:11

Foreever


This does exactly what you ask: remove last two chars of a string:

s.substr(0, s.length-2);
like image 17
Jochem Avatar answered Nov 15 '22 17:11

Jochem