Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How javascript converts float to int type
How javascript converts float to int type
There are two ways to convert a float type to an int type:

1. You can use the upward rounding (50/4) of Math.ceil, or the downward rounding (50/4) of math.floor, or the rounding (50/4) of Math.round.

2. Directly use the method of forced conversion to integer parseInt, parseInt(50/4).

1)parseInt? Function converts its first parameter to a string, parses it, and then returns an integer or NaN. If it is not NaN, the return value will be an integer as the first parameter of the number in the specified radix.

2)parseInt has two parameters, the first is the content to be converted, and the second is the decimal number to be converted, which is decimal by default. It can also be converted into octal, hexadecimal and binary. As follows:

3)parseInt can also convert "a string starting with a number" into a number, for example: Parseint ('12.0 swrf')//return:12.

4) There is another method, ParseFloat (string, radix), which is similar in usage and function to parseInt. ParseFloat forces a string or number to be converted to a floating-point type.

Extended data:

1, javascript is a weak language type, and there is no distinction between strings, integers and floating-point numbers when defining variables. However, it is flexible and can perform different types of transformations.

2. The basic data types of 2.javascript are: number, string, Boolean, undefined, empty and symbol, and variables can be forcibly converted into corresponding types according to the built-in objects of number, string, Boolean and symbol. As follows:

References:

MDN Technical Documentation -parseInt Syntax

Data type and data structure of MDN technical document js