Javascript numbers keep digits after decimal point

Source: Internet
Author: User

We can see that many people want to retain the decimal point after the number, but most of them write a function to intercept the number, and consider rounding it down. It is quite complicated to write it.

In fact, the javascript Number object has a method to retain decimal places: toFixed, which is the Number after rounding.

I was worried that IE6 did not support this method at one time. As shown in MDN, this method was implemented only by javascript1.5. I tried it in IE6 and it is fully supported.

ToExponential ([fractionDigits]): returns the number in scientific notation, the number of digits retained after the decimal point in the fractionDigits value.

ToFixed ([fractionDigits]): returns the number based on the specified number of decimal places. The fractionDigits value is the number of digits that are retained after the decimal point.

ToPrecision ([precision]): returns a number based on the specified precision (this precision is not the number after the decimal point), where precision is the specified precision value.

Example:

The Code is as follows:

Var n = 12345.6789;

N. toFixed (); // Returns 12346

N. toFixed (1); // Returns 12345.7

N. toFixed (6); // Returns 12345.678900

(1.23e + 20). toFixed (2); // Returns 123000000000000000000.00

(1.23e-10). toFixed (2); // Returns 0.00

2.34.toFixed (1); // Returns 2.3

-2.34.toFixed (1); // Returns-2.3

(-2.24). toFixed (1); // Returns-2.2

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)

}

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.