Alex Rivera | Logout

How to delete "px" from 245px

Asked 2011-02-01T08:09:47.830
92

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

Edit
Report

2 Answers

215

To convert 245px in 245 just run:

parseInt('245px', 10);

It retains only leading numbers and discards all the rest.

answered 2011-02-01T08:10:50.563
44

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

answered 2011-02-01T08:13:26.780

Your Answer