JS set percentage reserved two decimal places

Source: Internet
Author: User

createtime--2017 August 23 11:03:31
Author:marydon

JS set percentage reserved two decimal places

Error usage:

var percent = (num1/num2) * 100%;

Correct usage:

var percent = Math.Round (NUM1/NUM2 * 10000)/100 + "%";

Description

1.math.round (num) indicates that NUM is rounded, preserving only the integer digits, such as num=0.49, and returning a result of 0, such as num=0.51, which returns a result of 1;

2. Convert the results to percentages and retain 2 decimal places:

Ideas:

2.1 will enlarge the result of num1/num2 100 times times;

2.2 retains 2 decimal places for magnified results.

2.3 retains 2 decimal places and needs to use the Math.Round (num) function;

2.4 because the Math.Round (NUM) function retains only the integer digits, it needs to be magnified 100 times times at least again;

2.5 After rounding, divided by the magnification of multiples, stitching on the '% ' can be.

Example:

num1/num2=0.57823--> magnification 100 times times:57.823--> need to retain 2 bits, at least 100 times times more magnification:5782.3--> use Math.Round () for rounding operations:5782--> Zoom in and out again (+):57.82--> stitching percent sign: 57.82%

JS set percentage reserved 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.