How to convert javascript string to number

It makes no difference what type of variable is used in the expression. If the expression is mathematical, all its variables will automatically be interpreted as numeric. If strings are processed, then all the "participants" of the expression are treated as strings. However, the JavaScript string-to-number conversion task exists in a much wider context.

JavaScript methods for converting strings to numbers

The arsenal of methods for converting strings to numbers is not large, but sufficient in all simple cases. Here JavaScript (especially for beginners) is the way from simple to complex using practical examples.

The example describes four different lines. In the first block of output, the type of each variable is determined by the typeof function as string. Then each line is very easily converted to a number. In the second block of output, changes in variables after conversion are visible, their type has become a number. JavaScript conversion example parseFloat is especially indicative: it was "12e + 3", it became "12000".

Range of string to number conversion functions

Changes in converting a string to a number can be significant! But only the first characters matter: they must be digital. If there are no digits, the result will be NaN.

The inverse conversion of a string that has become a number is not always the same string. This moment can be used to verify the correctness of the input of numerical information.

Conventional Conversion Methods

There are integers and there are fractional ones, respectively, JavaScript converts a string into a number by:

  • parseInt;
  • parseFloat.

The general case is realized by using a string in a simple mathematical expression.

It is enough to put a “+” sign in front of the character string and, if it has a number, then the result of the expression will be a number. The value of the variable may change, but the type will always change: typeof will show number, not string. It is important to understand that using a converted variable in a string expression can show a completely different result.

JavaScript for beginners in this context is extremely simple. It is more difficult to understand the operation of the integer conversion using the pasrseInt method, since it automatically works in the decimal number system, but can interpret the string as octal or hexadecimal. However, this fact does not always depend on the second parameter, which indicates the number system.

JavaScript will always turn a string into a number, but if there is no digital character in the string at the beginning of the string, then the result will be NaN.

You need to have an understanding of number systems, how to write hexadecimal (a number starts with '0x') and octal numbers (a number starts with '0').

javascript function

To understand the nuances of the parseFloat JavaScript method, it is enough to have an idea of ​​what a mathematical notation of a real number is.

Convert to sort

JavaScript is a browser language, therefore it is more critical than other languages ​​for characters outside the basic set of the Latin alphabet and numbers. Sorting is a popular operation. But it doesn’t always make sense to send data to the server for sorting purposes; it’s easier and more practical to do work on the spot in the browser.

To solve this problem, you can convert string characters to their numeric codes or assign an ordered sequence of digits to letters and numbers. The charCodeAt () method applied to the string [var iB = 'abcd'.charCodeAt (1)] will assign the numeric value 98 to the iB variable, that is, the code of the letter' b '. Given that the code value of the letter 'a' is 97, you can get the numbers of all the letters of the Latin alphabet in ascending order of lowercase and uppercase sets. Similarly spelled the Russian alphabet.

Own option of sorting through numbers allows you to create the desired character sets. You can, for example, “re-arrange” the Cyrillic alphabet and Latin alphabet or mix them in order to leave only letters that are different in spelling, add tab and space characters to the sets.

Generating a unique row number

javascript for beginners

If the letter code 'a' is 97, then the difference between the letter and the number 97 will give a unique letter number in the alphabet. Summing unique numbers for each character in a string, it is difficult to get a unique number for that string.

If each position of the letter in the line is assigned a weight, for example, the position:

  • 0 weight 1;
  • 1 weight 10;
  • 2 weight 100;
  • ...,

then multiplying the unique number of each character of the line by the weight of the position in which it is found, and summing all the numbers you can get a unique number and use it as a one-to-one correspondence with the original line.

Such a conversion of a string into a number is reversible, that is, by the number you can always get the original string. Such a conversion is advantageous because any operation can be safely done with a number in the context of encoding, Cyrillic and other local features of the site page, scope, country of the visitor.

javascript substring

"Growing" site page selectors

Often there is the task of creating selectors on the pages of the site, the values ​​of which cannot be specified in advance, but over time they are supplemented. In the very first application, an empty selector is available to the first visitor to enter information.

Each new input of a line of information into the selector (by any visitor) is transformed into a number, which, together with the original, is sent to the server for storage. When a new work session begins or a new visitor arrives, the selector is no longer empty. The page at loading comes to the browser with a non-empty selector.

With each new value of the selector, only once it is sent to the server for storage and only once is a unique digital code assigned to it.

To solve this problem, the JavaScript string method cannot be used as a number. The usual methods parseInt and parseFloat are designed for a different application, but you can come up with an algorithm to uniquely convert a string to a number, and not necessarily reversible. It is enough that the conversion algorithm will not be repeated on different sets of characters in a string.

javascript parsefloat

Traffic Optimization and Analytics

When creating a page, the developer uses significant amounts of information. Providing the visitor with the opportunity to enter information is a good way to downgrade the site due to its poor functionality and disappoint the visitor.

Assigning an event handler in the form of a JavaScript function for certain blocks of information for visitor’s actions, you can formulate a filter that allows the visitor to precisely set a goal, find the necessary information, and obtain the desired solution.

The conversion of line information here can be arbitrarily capacious in part of the line and very small in part of the number. In other words, the developer performs the conversion of JavaScript string to number according to his own algorithm. The visitor manipulates understandable information, and the minimum amount of data - the number - goes to the server.

The dynamics of the set of numbers for all visitors in the context of well-known information allows another JavaScript function (not a handler) called by the server’s response through the AJAX mechanism to quickly and real-time provide all visitors with the necessary information simultaneously. This is how the system works.

This option to convert JavaScript string to number is very popular in the development of online games, interactive conferences, instant messaging and so on.

Instrumental application of transformations

JavaScript and CSS in the context of processing numerical information allow you to control the display of the page without server involvement. CSS rules are constructed as substrings, recursively. Typically, a parameter is a number followed by several letters (for example, "px", "pt", "em", ...). A parameter is a substring in a rule, and a rule is a substring in the style of the class or identifier.

javascript css

JavaScript recursion. Substring. Substring ... goes to the right number, converts from a string to a number, changes it and writes it back to the right place. The rule is changed "automatically." It is simple and convenient, no server involvement.

Source: https://habr.com/ru/post/K15892/


All Articles