Find code to convert String to Integer using jQuery. To convert, use JavaScript parseInt() function which parses a string and returns an integer.
Related Post:
- jQuery to round off decimal values
- jQuery Code: Change text to Uppercase
The syntax of parseInt() function is,
It takes 2 parameters. First parameter is value which needs to be converted. And second parameter «radix» is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a integer number.
If the radix parameter is omitted, JavaScript assumes the following:
- If the string begins with «0x», the radix is 16 (hexadecimal)
- If the string begins with any other value, the radix is 10 (decimal)
If you string contains spaces then only first number will be returned.
This function will only return «NaN», if the first character cannot be converted to a number.
Feel free to contact me for any help related to jQuery, I will gladly help you.
Содержание
- Virendra
- 5 Answers 5
- JavaScript: преобразование string в integer
Virendra
Nothing shocks me, I’m a Software Engineer. And I am not young enough to know everything. I live in World Wide Web and from there take care of this website. This website communicates about my work, learning and experience. I believe life is short, and it is for loving, sharing, learning and connecting. So lets connect..
How can I take a sting containing numbers such as
and convert each number into a integer?
I have tried the following, but it just returns the first integer.
5 Answers 5
That is because string.split returns an array of strings.
If you want to handle each individual value returned by split , loop over the array and parse the items as numbers while iterating.
You can then do with the parsed number as you will. (in this example, it multiplies every number by two and appends the output)
parseInt will convert a single string, not an array of strings.
You can use jquery $.each to parse each item and return an array of int .
(putting this into html as in the question and the snippet below doesn’t really mean much as it will convert back to string for the html, but the values can be manipulated once in the array).
JavaScript: преобразование string в integer
Преобразовать строку в целочисленное значение в JavaScript очень просто. Для этого есть специальная функция parseInt().
Пример:
var w ;
var w > // w >
Также существует функция для преобразования в число с плавающей точкой (float) — parseFloat().
Примеры:
// parseInt():
var i = parseInt(«100px», 10); // i=100
var i = parseInt(«0xA»); //i=10
var i = parseInt(«10.5», 10); // i=10
var i = parseInt(«text 10», 10); // i=NaN
var i = parseInt(«AF», 16); // i=175
var i = parseInt(«10», 2); // i=2
var i = parseInt(«10», 8); // i=8
var i = parseInt(«10», 10); // i=10
var i = parseInt(«010»); // i=8
var i = parseInt(«010», 8); // i=8
var i = parseInt(«010», 10); // i=10
// parseFloat():
var f = parseFloat(«100px»); // f=100.0
var f = parseFloat(«0xA»); // f=NaN
var f = parseFloat(«10.5»); // f=10.5
var f = parseFloat(«10.5.7»); // f=10.5
var f = parseFloat(«0908»); // f=908
var f = parseFloat(«text 10»); // f=NaN
Источник: