Home >Web Front-end >JS Tutorial >How to convert characters into numbers in javascript

How to convert characters into numbers in javascript

青灯夜游
青灯夜游Original
2021-10-25 16:09:452899browse

Conversion method: 1. Use Number() function, syntax "Number(value)"; 2. Use parseInt() function, syntax "parseInt("value")"; 3. Use parseFloat() function , syntax "parseFloat("value")".

How to convert characters into numbers in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript converts characters into numbers

Method 1: Use the Number() function

Syntax: Number(value)

Number() can convert the parameter value into a number.

console.log(Number("1"));  //返回数值1
console.log(Number(true));  //返回数值1

Method 2: Use the parseInt() function

parseInt() can convert the value into an integer.

console.log(parseInt("123abc"));  //返回数字123
console.log(parseInt("1.73"));   //返回数字1

Method 3: Use the parseFloat() function

parseFloat() can convert the value to a floating point number.

console.log(parseFloat("1.234.5"));  //返回数值 1.234

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to convert characters into numbers in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn