Format decimal to retain two-bit _javascript tips after a decimal point

Source: Internet
Author: User
In the development process, you often encounter formatting to adjust decimals, such as two digits after the decimal point, and so on. Methods are also quite common, as is the memo below.

The first, using Math.Round

var original=28.453
1)//round "original" to two decimals
var result=math.round (original*100)/100; Returns 28.45
2)//Round "original" to 1 decimal
var result=math.round (original*10)/10; Returns 28.5

The second, js1.5 above can be used tofixed (x), you can specify the number of intercepted decimal point x

3)//round "original" to two decimals
var result=original.tofixed (2); Returns 28.45

4)//Round "original" to 1 decimal
var result=original.tofixed (1); Returns 28.5

The above two methods are most common, but can not meet certain special requirements, such as retaining two digits after the decimal point, if less than two, less than two bit 0. Now there is a third way.

Third, the conversion function, this code from a foreign forum.
Copy Code code as follows:

function Roundnumber (number,decimals) {
var newstring;//the new rounded number
Decimals = number (decimals);
if (Decimals < 1) {
NewString = (Math.Round (number)). ToString ();
} else {
var numstring = number.tostring ();
if (Numstring.lastindexof (".") = = 1) {//If there no decimal point
Numstring + = ".";/ /Give it one at the end
}
var cutoff = Numstring.lastindexof (".") + decimals;//the point in which to truncate the number
var D1 = number (numstring.substring (cutoff,cutoff+1));/The value of the last decimal place, we ' ll end
var d2 = number (numstring.substring (cutoff+1,cutoff+2));/The next decimal, after the last one we want
if (D2 >= 5) {//Do we need to round up on all? If not, the string would just be truncated
if (D1 = = 9 && cutoff > 0) {//If the digit is 9, find a new cutoff point
while (Cutoff > 0 && (d1 = 9 | | | isNaN (d1))) {
if (D1!= ".") {
cutoff-= 1;
D1 = number (numstring.substring (cutoff,cutoff+1));
} else {
cutoff-= 1;
}
}
}
D1 + 1;
}
if (D1 = = 10) {
numstring = numstring.substring (0, Numstring.lastindexof ("."));
var roundednum = number (numstring) + 1;
NewString = roundednum.tostring () + '. ';
} else {
NewString = numstring.substring (0,cutoff) + d1.tostring ();
}
}
if (Newstring.lastindexof (".") = = 1) {//Do this again, to the new string
NewString + = ".";
}
var decs = (newstring.substring (Newstring.lastindexof (".") +1)). length;
for (Var i=0;i<decimals-decs;i++) newstring + = "0";
var newnumber = number (newstring)//Make it a number if
Document.roundform.roundedfield.value = newstring; Output to the form field (change for your purposes)
}

5)//round "original" to two decimals
var result=original.tofixed (2); Returns 28.45

6)//Round "original" to 1 decimal
var result=original.tofixed (1); Returns 28.5

var original=28.4
var result=original.tofixed (2); Returns 28.40

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.