# How to adjust the decimal format and retain the second decimal point _ javascript tips-js tutorial

Source: Internet
Author: User
Adjust the decimal format, such as retaining the last two digits of the decimal point, the following articles collect some good implementation methods and share some of them. During the development process, we often encounter the need to adjust the decimal format, such as retaining the last two digits of the decimal point. The method is also quite common, as follows.

First, use 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

Second, you can use toFixed (x) above js1.5 to specify a number to take the x digits after the decimal point

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 the most common but cannot meet certain special requirements. For example, if the number of digits after the decimal point is retained, if the number is less than two and the number is less than two, the value is set to zero. The third method is available.

Third, the conversion function. This code is from a foreign forum.

The Code is 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 is no decimal point
NumString + = "."; // give it one at the end
}
Var cutoff = numString. lastIndexOf (".") + decimals; // The point at which to truncate the number
Var d1 = Number (numString. substring (cutoff, cutoff + 1); // The value of the last decimal place that we'll end up
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 at all? If not, the string will just be truncated
If (d1 = 9 & cutoff> 0) {// If the last 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 // Var newNumber = Number (newString); // make it a number if you like
Document. roundform. roundedfield. value = newString; // Output the result 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
Related Keywords:
Related Article

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.