Format the value after rounding, with a kilobytes, rounded to kilobytes

Source: Internet
Author: User

Format the value after rounding, with a kilobytes, rounded to kilobytes

<Html> 



Javascript formatting numbers, thousands of characters, retaining decimals, rounding

/*
Round the value and format it.
@ Param num value (Number or String)
@ Param cent: Number of decimal places to be retained)
@ Param isThousand whether the Sub-location is required: 0: not required, 1: required (value type );
@ Return: a string in the format of '20140901'
@ Type String
*/
Function formatNumber (num, cent, isThousand ){
Num = num. toString (). replace (/\ $ | \,/g ,'');
If (isNaN (num) // check that the input value is of the numerical type.
Num = "0 ";
If (isNaN (cent) // make sure that the input decimal point is a numeric value.
Cent = 0;
Cent = parseInt (cent );
Cent = Math. abs (cent); // obtain the number of decimal places and make sure it is a positive integer.
If (isNaN (isThousand) // make sure that the input must be of the numerical type in kilobytes.
IsThousand = 0;
IsThousand = parseInt (isThousand );
If (isThousand <0)
IsThousand = 0;
If (isThousand> = 1) // make sure that the input value is only 0 or 1
IsThousand = 1;
Sign = (num = Math. abs (num); // obtain the symbol (positive/negative)
// Math. floor: returns the maximum integer that is less than or equal to the value.
Num = Math. floor (num * Math. pow (10, cent) + 0.50000000001); // converts the specified decimal point to an integer first, and rounding out unnecessary decimal places.
Cents = num % Math. pow (10, cent); // obtain the decimal point.
Num = Math. floor (num/Math. pow (10, cent). toString (); // obtain the integer.
Cents = cents. toString (); // converts a decimal point to a string to evaluate the decimal point length.
While (cents. length <cent) {// complement the decimal point to the specified number of digits.
Cents = "0" + cents;
}
If (isThousand = 0) // you do not need a kilobytes of characters.
Return (sign )? '': '-') + Num + '.' + cents );
// Format the integer in kilobytes.
For (var I = 0; I <Math. floor (num. length-(1 + I)/3); I ++)
Num = num. substring (0, num. length-(4 * I + 3) + ''' +
Num. substring (num. length-(4 * I + 3 ));
Return (sign )? <G id = "1"> </G>

If x is a positive real number, the number of sub-places is rounded to five. What is the expression for retaining two decimal places?

Select A because the sub-Location corresponds to 0.001. If the number in the sub-location is less than 0.005, then add 0.005 and multiply it by 100 to be less than 1, so that the int operation will discard it, that is, "four homes". If the number of sub-bits is greater than 0.005, add 0.005 and multiply it by 100 and greater than 1. In this way, the int operation will "Five in" The value of the sub-bits ", add 1 to the percentile.

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.