current position:Home>[JavaScript] convert numerical value to numerical value
[JavaScript] convert numerical value to numerical value
2022-04-29 05:24:50【51CTO】
number():
Convert a numeric value to a numeric value
If it's a Boolean value , Or be converted to 1, Or be converted to 0
If it's a number , So how much should it be , How much?
If it is null value , Will be converted to 0
If it is undefined, Will be converted to NaN
String to number :
If the string contains only numbers , Then it will only be converted to decimal
If the string is empty , Will be converted to 0
If there are letters , Then it will be converted to NaN
If the string is octal , Then you will ignore the previous 0, But if it is 16 In the case of decimal system , Will be converted to the corresponding decimal number
parseInt():
This function will look more at whether there are numbers , If there is, it will be converted into numbers ; If the string is empty , Will be converted to NaN. The other thing is that , If it is 3.14, Then it will be converted into 3.
in fact , We parseInt() The function provides a second argument , Specifies the conversion to hexadecimal .
parseFloat():
This function can only parse 10 Base number , So there is no second parameter . Converts a string with a decimal point to a decimal point
stay JavaScript in , There are generally three ways of numerical conversion :
One 、Number(param) function :param Can be used for any data type
1.1 param yes Boolean value ,true and false Convert to 1 and 0;
1.2 param Numerical value , It's just a simple pass in and return
1.3 param yes null and undefined, Return respectively 0 and NaN
toString() Method : With character string Returns an array of , All number methods can be used for any type of number .
var x = 11;
x.toString(); // Dependent variable x return 11
(11).toString(); // From the text 11 return 11
(10 + 1).toString(); // From expression 10 + 1 return 11
- 1.
- 2.
- 3.
- 4.

Number() : Return to digital , From the transformation of its parameters . Can be used to JS Variables are converted to numeric values :
x = true;
Number(x); // return 1
x = false;
Number(x); // return 0
x = new Date();
Number(x); // return 1404568027739
x = "10"
Number(x); // return 10
x = "10 20"
Number(x); // Can't convert to numbers Then return to NaN
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.

parseInt() Conversion rules
because Number() Function in the conversion of string is more complex and unreasonable , So when dealing with integers, it's more common to use \
parseInt() function .parseInt() Function to convert a string , More to see if it fits the numerical model . It ignores words \
The space before the string , Until you find the first non space character . If the first character is not a numeric character ,parseInt()\
It will return NaN; in other words , use parseInt() Converting an empty string returns NaN(Number() Returns... For null characters 0). Such as \
If the first character is a numeric character ,parseInt() Will continue to parse the second character , Until all subsequent characters are resolved or encountered \
A non numeric character . for example ,"1234blue" Will be converted to 1234, because "blue" Will be completely ignored . Similarly ,"22.5"\
Will be converted to 22, Because the decimal point is not a valid numeric character .
The specified cardinality will affect the output of the transformation . for example :
var num1 = parseInt("10", 2); //2 ( Parse by binary )
var num2 = parseInt("10", 8); //8 ( Parse in octal )
var num3 = parseInt("10", 10); //10 ( Decimals )
var num4 = parseInt("10", 16); //16 ( Parse in hexadecimal )
- 1.
- 2.
- 3.
- 4.

\
copyright notice
author[51CTO],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/116/202204261035255827.html
The sidebar is recommended
- Rich text editor: ckeditor (using ckeditor4 Vue)
- The get request of nginx agent only returns part of the data. The problem is solved
- JavaScript traverses the irregularly nested multi-layer objects, returns the map with "index", and realizes the rapid positioning of sub attributes
- HTTP keep alive details
- [technical update] http / 3 quic Foundation
- Vue to react ----- can the constructor be omitted when using ES6?
- Use of nested HTML
- Vue to react to realize slot function
- When to use react PureComponent
- React 16.6 memo()
guess what you like
Deep understanding of children https://segmentfault.com/a/1190000011527160
This paper solves the cross domain problem Vue + springboot caused by the separation of front and back ends
The difference between shallow copy and deep copy is to use native JavaScript to realize deep copy
Definition of Vue slot
Sorting algorithm in JavaScript
JavaScript implements search algorithm, sequential search and binary search
leetcode20. Valid parentheses, implemented using JavaScript
'interface' declarations can only be used in typescript files in Vue 3.0 + ts resolvent
496 next element larger JavaScript implementation leetcode
leetcode 682. Implementation of JavaScript in baseball game
Random recommended
- Source code analysis, Vue What happens when using (), take initializing vuex as an example
- JavaScript optional chain
- Source code analysis, Vue global API set, del, nexttick, use, mixin, etc
- Brief introduction of Vue source code directory design
- Docxtemplator + Vue + docxtemplator image module free realize the front-end Word Download Text and free image download
- vue 2. What happens when x source code analyzes new vue()
- Vue bidirectional binding principle
- Use and principle of Vue keepalive
- Properties and usage of vuex
- Communication mode between Vue components
- Front end interview Foundation
- Usage scenario and principle of Vue nexttick
- Configuration and use of rich text editor ckeditor4 in Vue
- How does Java randomly get elements from a list
- Study summary of pytext: a search path from NLP research to production
- html+css+JavaScript
- Tunnel http://bt3xna.natappfree.cc Invalid cannot connect to 172.0.0.1:8080 The web service powered by natapp. Is not available on this port cn
- Next JS using react quill
- Vue Gaode map can only have one start point and end point. How to realize it
- Divide and conquer strategy -- find the k-th smallest element in the array
- Singleton mode - front end design mode
- QT sets that child elements do not inherit the parent style
- Using react to realize comment function
- Front end Vue template
- Front end vuejs set sail
- Front end Vue data binding and MVVM
- Vue data proxy and hijacking in front end
- Vue style of front end
- Front end Vue event handling
- Principle of Vue monitoring data change in front end
- Vue calculation attribute and monitoring attribute of the front end
- Front end Vue conditional rendering and list rendering
- The front-end Vue collects form data
- Vue built-in instruction of front end
- Unknown HttpClient factory netty at org. openqa. selenium. remote...
- What is the gutter principle of Ant Design Vue a-row
- Element form account password
- CSS to add horizontal lines around the text
- Solution to vscode Vue project always reporting space or semicolon warning
- Calling setcookie in Ajax fails without any error in the IDE