How to convert a string into a number in JavaScript

How to convert a string into a number in JavaScript

The main methods are: 1. parseInt() ; 2. Number() ; 3. parseFloat() ;

Let's introduce them one by one:

1.parseInt(string, radix)

  • Parses a string and returns a decimal integer in the specified radix or NaN.
  • The first argument is the value to be parsed. If the argument is not a number, it is converted to a number;
  • The second parameter specifies the base of the parsed value.
  • If the first character passed in cannot be converted to a number, parseInt returns NaN.

Here is a compatibility issue:

If radix is undefined , 0 , or unspecified, JavaScript assumes the following:

  • If the input string begins with "0x" or "0x" (a 0 followed by a lowercase or uppercase X), radix is ​​assumed to be 16 and the rest of the string is parsed as a hexadecimal number.
  • If the input string begins with "0" (0), radix is ​​assumed to be 8 (octal) or 10 (decimal). The exact radix chosen is implementation dependent. ECMAScript 5 clarified that 10 (decimal) should be used, but not all browsers support this yet. Therefore, when using parseInt , be sure to specify a radix.
  • If the input string begins with any other value, radix is ​​10 (decimal).

2. Number()

Function converts the value of an object to a number

  • Number() function converts the value of an object to a number.
  • When a string is passed to Number() conversion function, it will try to convert it to an integer or floating point number directly. This method can only convert based on decimal. If non-numeric characters appear in the string, NaN will be returned.

3.parseFloat()

  • The given value is parsed as a floating point number or integer. If it cannot be converted to a number, NaN is returned.
  • parseFloat is a global function and does not belong to any object.

All can be used directly:

function circumference(r) {
  return parseFloat(r) * 2.0
}
 
console.log(circumference(3));
// Output: 6

Finally, there is a more powerful operation:

數字字符串前直接寫“+”直接轉換

This is the end of this article about how to convert JavaScript strings to numbers. For more relevant JavaScript string conversion to numbers content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of Javascript basics loop
  • How to use async and await correctly in JS loops
  • Four data type judgment methods in JS
  • About the difference between js typeof and instanceof in judging data types and their development and use
  • An example of elegant writing of judgment in JavaScript
  • parseInt parseFloat js string conversion number
  • JavaScript common statements loop, judgment, string to number

<<:  A brief discussion on DDL and DML in MySQL

>>:  Share the responsive frameworks commonly used by web design masters (summary)

Recommend

CSS horizontal progress bar and vertical progress bar implementation code

Sometimes it’s nice to see some nice scroll bar e...

The difference and reasons between the MySQL query conditions not in and in

Write a SQL first SELECT DISTINCT from_id FROM co...

Have you carefully understood Tags How it is defined How to use

Preface : Today I was asked, "Have you carefu...

Linux system file sharing samba configuration tutorial

Table of contents Uninstall and install samba Cre...

Summary of Nginx location and proxy_pass path configuration issues

Table of contents 1. Basic configuration of Nginx...

Docker core and specific use of installation

1. What is Docker? (1) Docker is an open source t...

A brief discussion on HTML table tags

Mainly discuss its structure and some important pr...

Detailed explanation of Nginx timed log cutting

Preface By default, Nginx logs are written to a f...

js realizes the dynamic loading of data by waterfall flow bottoming out

This article shares with you the specific code of...

Introduction to the use of em in elastic layout in CSS3: How many pixels is 1em?

I have been using CSS for a long time, but I have...

Detailed explanation of server-id example in MySQL master-slave synchronization

Preface When we build a MySQL cluster, we natural...

Example of implementing the Graphql interface in Vue

Note: This article is about the basic knowledge p...

Detailed steps for installing MySQL using cluster rpm

Install MySQL database a) Download the MySQL sour...