JavaScript advanced (5) functions in JavaScript that take fractional Integers

Source: Internet
Author: User
This article describes the details of functions that take the fractional integer part in JavaScript advanced (5) js. If you want to use this function, you can refer to the following JavaScript to take the fractional integer part. The function discards the fractional part and retains the integer part.

Js: parseInt (7/2)

Rounded up. If there is a decimal number, add 1 to the integer.

Js: Math. ceil (7/2)

Rounding

Js: Math. round (7/2)

Round down

Js: Math. floor (7/2)

Appendix: how to determine the type of the input value (without quotation marks !)

The regular expression is used.

"^ \ D + $" // non-negative integer (positive integer + 0)

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

"^ (-\ D +) | (0 +) $" // non-positive integer (negative integer + 0)

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

"^ -? \ D + $ "// integer

"^ \ D + (\. \ d + )? $ "// 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

"^ (-\ D + (\. \ d + )?) | (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 returns true or false for the characters to be judged.

Meiwenmeitu

The above is the content. For more information, please follow the PHP Chinese Network (www.php1.cn )!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.