Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Common functions of JS digital calculation and processing
Common functions of JS digital calculation and processing
1, digital conversion

1, Number(object): converts an object into a number. If the parameter is a Date object, Number () returns the number of milliseconds between 1970 65438+ 10 month 1. If the value of the object cannot be converted to a Number, the number () function returns NaN.

When added to JS, the result will always become a string connection. This function is used to solve this problem, such as: var c = number (0)+number (1);

It is suggested to write like this. The premise is that you must ensure that the object can be converted into numbers.

2.isNaN (): judge whether the parameter is non-numeric, if it is numeric, return false, if it is not numeric, return true;; But when the parameter is an empty string and a string full of spaces, the returned result is also false;;

3.parseFloat (): Parses string parameters and returns floating-point numbers. First, it ignores the spaces at the beginning and end of the string, and then determines whether the first character in the string is a number. If so, the string will be parsed until it reaches the end of the number, and then the number will be returned as a number instead of a string. If the first string is not a number, it is returned as NaN; For example: parse float ('40.01ABC'); The return value is 40.0 1, parseflow ('a40.01BC'); The return value is NaN, and the return value is not necessarily expressed in decimal, for example: parse float ('40 ABC'); The return value is 40.

4.parseInt (): Parses the string parameter into a signed decimal integer. (The parsing process is similar to parser (), which ignores leading and trailing spaces and parses from the first character ...) If there are two parameters, the string parameter is parsed into a signed integer using the base of the second parameter. For example: parseint ("9a"); The return value is 9, parseInt(" 1 1c? " ,2); The return value is 3,' 1 1C?' Convert binary number 1 1 first, and then convert it into decimal number. The binary number 1 1 corresponds to the binary number 10 as 3.

Second, keep 2 as a decimal:

1, rounding

var num = 2.446242342num = num . to fixed(2); ? //The output result is 2.45.

2. No rounding

The following processing results will not be rounded.

First, change the decimal to an integer:

math . floor( 15.77845 14000 * 100)/ 100? //The output result is 15.77.

Second, as a string, use regular matching:

No. (15.7784514000.tostring ()). match(/^\d+(? :\.\d{0,2})? /)); The output result is 15.77. Cannot be used for integers, such as 10. Must be written as 10.0000. If it is a negative number, please convert it into a positive number and then calculate it, and finally turn it back to a negative number.

Math.floor () is prone to precision problems, such as decimal 8.54 retaining two decimal places (although it has retained two decimal places), mathematics. Floor (8.54 *100)/100; The output result is 8.53. Note that it is 8.53, not 8.54. Therefore, this function should be used with caution.