Convert a string to an Int (integer) with JavaScript

This section describes the procedure for converting a string data type to an integer data type. JavaScript.

JavaScript has a function called parseInt (). It is used to parse a string as an argument and returns an integer of the specified radix (basically a notational radix) as output.

Now see the code implementation for this in JavaScript.

Function stringToIntScale (x, base) {

const parsed = parseInt (x, base);

if (isNaN (parsed)) {return 0;}

Returns parsed * 100;

console.log (stringToIntScale (‘0xF’, 16));

// Expected output is 1500

console.log (stringToIntScale (‘321’, 2));

// Expected output is: 0

Therefore, the syntax for using parseInt () is:

parseInt (string)parseInt (string, radix)Now find out what parameters you need to pass to this function.Therefore, the first parameter is a String, which is the value to be parsed. If the passed argument is not a string, it will be converted to a string using an operation called toString.If this argument is preceded by a blank, it will be ignored.The second parameter is an optional parameter that is radix. radix Is the basis of mathematical notation. The radix is ​​expressed in the range of integers from 2 to 36. Therefore, this is how to represent the radix of a string. If the radix value is not a numeric type, it will be modified to a numeric value.Then look at the return value.If a string is specified as an argument to parseInt, the integer will be parsed.However, in some cases, it can be NaN as follows: The radix modulo 2 ** 32 is greater than 36 and less than 2. The first non-blank character is not converted to a number. Check the description of this parseInt () here.Its basic functions are as follows: First, the argument is converted to a string. Then the string is parsed. Returns either an integer or NaN. If the return value is not Nan, the return value will be an integer and the return value will be based on the first argument passed. Also, based on the radix value, if the radix value is 8, it is converted from octal, if the radix value is 16, it is converted from hexadecimal, and if the radix is ​​10, it is converted from decimal.If the radix is ​​greater than 10, numbers greater than 9 are indicated in the English alphabet. For example, A to F are used for hexadecimal (hexadecimal).If parseInt () encounters such a character during the conversion, this is not a number in the specified radix, parseInt ignores it and all subsequent characters, and finally parses to that point. Returns an integer value. Numbers to integer values ​​are truncated by parseInt.If desired, you can modify the value parsed as a radix argument to a number.It will be NaN or Infinity, as if the value was zero, and undefined will be corrected to NaN.Javascript assumes the following: If the radix value is assumed to be 16 and the input string starts with zero, followed by lowercase and uppercase letters, the rest of the string is parsed as hexadecimal. Otherwise, if the input string begins with […]

Leave a Reply

Your email address will not be published. Required fields are marked *