Proficient in the JavaScript series numerical computing _javascript techniques

Source: Internet
Author: User
A little more intuitive:
---------------------
Example: Var a=32.6;
var b=67;
var c=9e5;
---------------------
For numeric types, if you want to convert to scientific notation, you can use the Toexponential () method, which takes a parameter that represents the number of decimal times to output:
---------------------
Cases:
var a=78.9;
Alert (a.toexponential (1));
---------------------
The problem of formatting the numerical value in JS

Formatting a number is a very meaningful thing to do, for example, many times we want a number to be output as a string in a specified format. Take 26.9878来 said, we may want it to retain two decimal places, that is, the result is 26.99, or for 0.345678, you want to be able to output by percent, and retain two digits after the decimal point, the result is 34.57%, For example, we want to display the number 56456456 in scientific notation and keep two digits after the decimal point, which is 5.65e+7, and of course there are a lot of examples.

So is it possible to provide support for these formatted outputs in the JavaScript standard, which can be said to be partially supported by JavaScript, but provided with perfect, specific you can look at the use of Number objects, which provides some formatted output of numbers, Several functions are as follows:
Copy Code code as follows:

Toexponential ([fractiondigits]): Returns the number in scientific notation format, where the fractiondigits value is retained after the decimal point.
ToFixed ([fractiondigits]): Returns a number to the specified number of decimal places, where the fractiondigits value is retained after the decimal point.
Toprecision ([precision]): Returns a number to the specified precision (this precision does not refer to a few after the decimal point), where precision is the specified precision value.

If you don't use the above function, I'll simply give you an example:
Copy Code code as follows:

var num=56.45678;
var rs1=num.toexponential (2);//rs1 value is 5.65e+1
var rs2=num.tofixed (2);//rs value is 56.45
var rs3=num.toprecision (2);//rs value is 56

Although the number object provides these methods to solve many of the problem of digital transformation, but for many cases is not satisfied, such as the support of the hundred semicolon and so on.

To address this problem and provide a more powerful and flexible number format requirement, Jsjava specifically provides JavaScript custom classes for support, you can download jsjava-1.0.js and reference the

Src/jsjava/text/numberformat.js or direct reference to jslib/jsjava-1.0.js, for example, as follows:
Copy Code code as follows:

<script src= "Js/jsjava/text/numberformat.js" ></script>
<script src= "Js/jsjava/lang/stringbuffer.js" ></script>
<script src= "Js/jsjava/lang/illegalargumentexception.js" ></script>
<script>
var nf=new decimalformat ();
Nf.applypattern ("0%");
var Res=nf.format (-0.893566);
document.write (res+ "<br>");
Nf.applypattern ("0000.00");
var Res=nf.format (-53.385967);
document.write (res+ "<br>");
Nf.applypattern ("0000.000E00");
var res=nf.format (53.385967);
document.write (res+ "<br>");
</script>

Display results as:

-89.357%
-53.39
5338.597e-2
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.