How to use javascript to get float decimal digits _ javascript tips-js tutorial

Source: Internet
Author: User
In js, the most common method for getting the decimal point and the two decimal points is the rounding function. I have previously introduced this common function in js, which is useful here, next, let's take a look at some methods to get the float decimal point and sum up the following: we will introduce the implementation of JavaScript to retain two decimal places:
Rounding
The following processing results are rounded up:

Var num = 2.446242342; num = num. toFixed (2); // The output result is 2.45.

Rounding
The following processing results are not rounded up:
First, add the decimal side integer:

Math. floor (15.7784514000*100)/100 // The output result is 15.77.

Second, use regular expression matching as a string:

Number (15.7784514000.toString (). match (/^ \ d + (? : \. \ D {0, 2 })? //) // The output result is 15.77, which cannot be used as an integer. For example, 10 must be written as 10.0000.

NOTE: If it is a negative number, convert it to a positive number before calculation, and then convert it back to a negative number.

Example of javascript retaining two decimal places:

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.