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

Vue two fields joint verification to achieve the password modification function

Table of contents 1. Introduction 2. Solution Imp...

React Router V6 Updates

Table of contents ReactRouterV6 Changes 1. <Sw...

Example of using store in vue3 to record scroll position

Table of contents Overall Effect Listen for conta...

HTML tutorial, easy to learn HTML language

1. <body background=image file name bgcolor=co...

CSS3 gradient background compatibility issues

When we make a gradient background color, we will...

Mysql sorting and paging (order by & limit) and existing pitfalls

Sorting query (order by) In e-commerce: We want t...

How to install Linux flash

How to install flash in Linux 1. Visit the flash ...

XHTML no longer uses some obsolete elements in HTML

When we do CSS web page layout, we all know that i...

Solve the problem of spring boot + jar packaging deployment tomcat 404 error

1. Spring boot does not support jsp jar package, ...

How to install MySQL for beginners (proven effective)

1. Software Download MySQL download and installat...

MySQL Basic Tutorial: Detailed Explanation of DML Statements

Table of contents DML statements 1. Insert record...

Detailed steps for setting up host Nginx + Docker WordPress Mysql

environment Linux 3.10.0-693.el7.x86_64 Docker ve...

Docker implements re-tagging and deleting the image of the original tag

The docker image id is unique and can physically ...

A brief discussion on CSS height collapse problem

Performance For example: HTML: <div class=&quo...

Detailed explanation of TIMESTAMPDIFF case in MySQL

1. Syntax TIMESTAMPDIFF(unit,begin,end); Returns ...