Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert numbers using strings in JavaScript

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces you how to use JavaScript strings to convert numbers, the content is very detailed, interested friends can refer to, I hope to help you.

There are three main ways to convert numbers to js strings: conversion functions, forced type conversion, and weak type conversion using js variables.

1. Conversion function:

js provides two conversion functions, parseInt() and parseFloat(). The former converts values to integers, while the latter converts values to floating-point numbers. Both functions work correctly only if these methods are called on String types; NaN(Not a Number) is returned for all other types.

Some examples are as follows:

parseInt("1234blue"); //returns 1234 parseInt("0xA"); //returns 10 parseInt("22.5"); //returns 22 parseInt("blue"); //returns NaN

The parseInt() method also has a base pattern that converts binary, octal, hexadecimal, or any other base string to an integer. The base is specified by the second argument of the parseInt() method, as follows:

parseInt("AF", 16); //returns 175 parseInt("10", 2); //returns 2 parseInt("10", 8); //returns 8 parseInt("10", 10); //returns 10

If decimal numbers contain leading zeros, it is best to use base 10 so that octal values are not accidentally obtained. For example:

parseInt("010"); //returns 8

parseInt("010", 8); //returns 8 parseInt("010", 10); //returns 10

The parseFloat() method works similarly to the parseInt() method. Another difference with the parseFloat() method is that strings must represent floating-point numbers in decimal form and parseFloat() has no base pattern.

Here is an example of using the parseFloat() method:

parseFloat("1234blue"); //returns 1234.0 parseFloat("0xA"); //returns NaN parseFloat("22.5"); //returns 22.5 parseFloat("22.34.5"); //returns 22.34 parseFloat("0908"); //returns 908 parseFloat("blue"); //returns NaN

In order to help you make learning easy and efficient, I will share a large amount of information for free to help you overcome difficulties on the road to becoming a front-end engineer and even a full-stack engineer. Here to recommend a front-end full stack learning button qun: 784783012 Welcome to the group. Exchange and discuss, learn and exchange, and make progress together.

When you really start learning, you inevitably don't know where to start, which leads to inefficiency and affects confidence in continuing learning.

However, the most important thing is that I don't know which technologies need to be mastered. I often step on pits when learning, and eventually waste a lot of time. Therefore, effective resources are still necessary.

Learning front, we are serious

2. Forced type conversion

You can also use type casting to handle the type of the converted value. Use coercion to access a particular value, even if it is of another type. The three mandatory type conversions available in ECMAScript are as follows:

Boolean(value)-converts the given value to Boolean;

Number(value)-converts a given value to a number (either integer or floating-point);

String(value)--converts the given value to a string.

Converting a value with one of these three functions creates a new value that stores the value converted directly from the original value. This can have unintended consequences. The Boolean() function returns true when the value to be converted is a string, a non-zero number, or an object with at least one character (discussed in the next section). If the value is an empty string, the number 0, undefined, or null, it returns false.

You can test Boolean coercion with the following code snippet.

Boolean(""); //false - empty string Boolean("hi"); //true - non-empty string Boolean(100); //true - non-zero number Boolean(null); //false - null Boolean(0); //false - zero Boolean(new Object()); //true - object

The cast of Number() is handled in a similar way to the parseInt() and parseFloat() methods, except that it converts the entire value rather than a partial value. Examples are as follows:

administration and Result Number(false) 0 Number(true) 1 Number(undefined) NaN Number(null) 0 Number( "5.5 ") 5.5 Number( "56 ") 56 Number( "5.6.7 ") NaN Number(new Object()) NaN Number(100) 100

The last coercion, String(), is the simplest, and an example is as follows:

var s1 = String(null); //"null" var oNull = null; var s2 = oNull.toString(); //won't work, causes an error3. Using js variable weak type conversion

Take a small example and you will understand.

var str= '012.345 '; var x = str-0; x = x*1;

The above example takes advantage of the weak type characteristics of js, only performs arithmetic operations, and realizes the type conversion from string to number, but this method is still not recommended.

About how to use JavaScript string conversion numbers to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report