Advanced JavaScript (5) Functions for taking decimals and integers in js

黄舟
Release: 2017-02-11 14:44:21
Original
1933 people have browsed it

Function to get the integer part of a decimal in js

Discard the decimal part,Keep the integer part

js:parseInt (7/2)

Round up,If there is a decimal, add the integer part1

js: Math.ceil(7/2)

Rounding

js: Math.round(7/2)

Rounding down

js: Math.floor(7/2)

Attachment: How to determine the input numerical type (use without quotes!)

In fact, it is to use regular expressions.

"^\\d+$" // Non-negative integer (positive integer+0)

"^[0-9]* [1-9][0-9]*$"//Positive integer

"^((-\\d+)|(0+))$" // Not positive Integer (negative integer+ 0)

"^-[0-9]*[1-9][0-9]*$"//Negative integers //

Non-negative floating point number (positive floating point number

+0

)"^(([0-9]+\\.[0- 9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9 ]*[1-9][0-9]*))$" //Positive floating point number ?)|(0+(\\.0+)?))$"//

Non-positive floating point number (negative floating point number

+0

"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9 ]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"//Negative floating point number"^(-?\\d+)(\\.\\d+)?$"//

Floating point number

Example

var r = /^[0-9]*[1-9][0-9]*$///Positive integer

r.test(str); //str

is the character you want to judge and the execution result is

trueor

false

## Beautiful text and beautiful pictures

##The above is the content. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!