Home > Web Front-end > JS Tutorial > js instructions for formatting numbers_javascript skills

js instructions for formatting numbers_javascript skills

WBOY
Release: 2016-05-16 18:12:03
Original
1145 people have browsed it

You can use
In js----------

Javascript is also provided Support for formatted output of numbers

Several formatting functions provided by the Number object:

toExponential([fractionDigits]): Return the number in scientific notation format, in which fractionDigits The number of digits after the decimal point of the value.
toFixed([fractionDigits]): Returns the number with the specified number of decimal places, where fractionDigits is the number of digits left after the decimal point.
toPrecision([precision]): Return the number to the specified precision (this precision does not refer to the number of decimal places), where precision is the specified precision value.

var num=56.45678;
var rs1=num.toExponential(2);//The value of rs1 is 5.65e 1
var rs2=num.toFixed(2);//The value of rs The value is 56.45
var rs3=num.toPrecision(2);//The value of rs is 56



Although the methods provided by the Number object can solve many number conversions problem, but it is still not satisfactory for many situations, such as support for percent signs and so on.

In order to solve this problem and provide more powerful and flexible digital formatting requirements, JsJava specifically provides Javascript custom classes for support. You can download jsjava-1.0. js, reference src/jsjava/text/NumberFormat.js or directly reference jslib/jsjava-1.0.js, for example:

Copy code The code is as follows:




<script> <br>var nf=new DecimalFormat(); <br>nf.applyPattern("000.000%"); <br>var res=nf.format(-0.893566); <br>document.write(res "<br>" ); <br>nf.applyPattern("0000.00"); <br>var res=nf.format(-53.385967); <br>document.write(res "<br>"); <br>nf.applyPattern ("0000.000E00"); <br>var res=nf.format(53.385967); <br>document.write(res "<br>"); <br></script>

The displayed result is:

-89.357%
-53.39
5338.597e-2
Related document download

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template