⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 number.html

📁 javascript 1.5规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<BR><A NAME="1206810"> </A><A NAME="Examples"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Examples</zrefheader></B></FONT><BR><A NAME="1206339"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>var num=77.1234<br>alert("num.toExponential() is " + num.toExponential()) //displays 7.71234e+1<br>alert("num.toExponential(4) is " + num.toExponential(4)) //displays 7.7123e+1&nbsp;&nbsp;&nbsp;<br>alert("num.toExponential(2) is " + num.toExponential(2)) //displays 7.71e+1<br>alert("77.1234.toExponential() is " + 77.1234.toExponential())<br>//displays 7.71234e+1<br>alert("77 .toExponential() is " + 77 .toExponential()) //displays 7.7e+1</zcodeline></FONT><P><BR><A NAME="1201287"> </A><A NAME="See also"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>See also</zrefheader></B></FONT><BR><A NAME="1201291"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><A HREF="number.html#1200964"  >toFixed</A>, <A HREF="number.html#1201389"  >toPrecision</A>, <A HREF="number.html#1193464"  >toString</A></zparagraph></FONT><P><BR><P><A NAME="1200964"> </A><A NAME="toFixed"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="4"><B><zrefhead2>toFixed</zrefhead2></B></FONT><P><A NAME="1200232"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Returns a string representing the Number object in fixed-point notation.<BR><P><TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0">  <CAPTION></CAPTION><P><FONT FACE=""><B></B></FONT><BR><BR>  <TR>    <TD VALIGN=TOP><P><A NAME="1200218"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><I>Method of</I></ztabletext></FONT>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1200223"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><A HREF="number.html#1193137"  >Number</A></ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><P><A NAME="1200225"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><I>Implemented in</I></ztabletext></FONT>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1200227"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext>JavaScript 1.5</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><P><A NAME="1200229"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><I>ECMA version</I></ztabletext></FONT>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1200231"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext>ECMA-262, Edition 3</ztabletext></FONT>&nbsp;</TD>  </TR></TABLE><P> </zparagraph></FONT><P><BR><A NAME="1200233"> </A><A NAME="Syntax"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Syntax</zrefheader></B></FONT><BR><A NAME="1200234"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>toFixed([<I>fractionDigits</I>])</zcodeline></FONT><P><BR><A NAME="1200235"> </A><A NAME="Parameters"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Parameters</zrefheader></B></FONT><BR><A NAME="1200241"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><BR><P><TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0">  <CAPTION></CAPTION><P><FONT FACE=""><B></B></FONT><BR><BR>  <TR>    <TD VALIGN=TOP><A NAME="1200341"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>fractionDigits</zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1200343"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext>An integer specifying the number of digits after the decimal point. Defaults to zero.</ztabletext></FONT>&nbsp;</TD>  </TR></TABLE><P></zparagraph></FONT><P><BR><A NAME="1200242"> </A><A NAME="Description"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Description</zrefheader></B></FONT><BR><A NAME="1200525"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">Number.prototype.toFixed</FONT> method returns a string representing a Number object in fixed-point notation, rounded to the number of digits after the decimal point specified by <FONT FACE="courier, couriernew, monospaced size"><I>fractionDigits</I></font>. </zparagraph></FONT><P><A NAME="1201067"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The output of <FONT FACE="courier, couriernew, monospaced size">toFixed</FONT> may be more precise than <FONT FACE="courier, couriernew, monospaced size">toString</FONT> for some values, because <FONT FACE="courier, couriernew, monospaced size">toString</FONT> outputs only enough significant digits to distinguish the number from adjacent number values.</zparagraph></FONT><P><A NAME="1207032"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>If a number has more digits that requested by the <FONT FACE="courier, couriernew, monospaced size"><I>fractionDigits</I></font> parameter, the number is rounded to the nearest number represented by <FONT FACE="courier, couriernew, monospaced size"><I>fractionDigits </I></font>digits. If the number is exactly halfway between two representable numbers, it is rounded away from zero (up if it is positive, down if it is negative). Thus:</zparagraph></FONT><P><dl>  <dl>     <DT> <A NAME="1207136"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><zparaindent1><FONT FACE="courier, couriernew, monospaced size">0.124.toFixed(2)</FONT> returns "0.12".</zparaindent1></FONT><P>     <DT> <A NAME="1207163"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><zparaindent1><FONT FACE="courier, couriernew, monospaced size">0.125.tofixed(2) </FONT>returns "0.13", because 0.125 is exactly halfway between 0.12 and 0.13.</zparaindent1></FONT><P>     <DT> <A NAME="1207201"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><zparaindent1>0<FONT FACE="courier, couriernew, monospaced size">.126.tofixed(2)</FONT> returns "0.13".</zparaindent1></FONT><P>  </dl></dl><A NAME="1207214"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Given this convention, one might expect <FONT FACE="courier, couriernew, monospaced size">0.045.toFixed(2)</FONT> to return "0.05", but it returns "0.04". This is because of the way computers represent IEEE 754 floating-point numbers. The IEEE 754 standard uses binary fractions (fractions of 0's and 1's after the dot). Just as some numbers, such as 1/3, are not representable precisely as decimal fractions, other numbers, such as 0.045, are not precisely representable as binary fractions. The IEEE 754 standard dictates that 0.045 be approximated to 0.04499999999999999833466546306226518936455249786376953125, which is precisely representable as a binary fraction. This approximation is closer to 0.04 than to 0.05,<FONT FACE="courier, couriernew, monospaced size"> </FONT>so <FONT FACE="courier, couriernew, monospaced size">0.045.toFixed(2)</FONT> returns "0.04".</zparagraph></FONT><P><BR><A NAME="1201068"> </A><A NAME="Examples"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Examples</zrefheader></B></FONT><BR><A NAME="1200863"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>var num=10.1234<br>alert("num.toFixed() is " + num.toFixed()) //displays 10<br>alert("num.toFixed(4) is " + num.toFixed(4)) //displays 10.1234&nbsp;&nbsp;&nbsp;alert("num.toFixed(2) is " + num.toFixed(2)) //displays 10.12</zcodeline></FONT><P><BR><A NAME="1201348"> </A><A NAME="See also"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>See also</zrefheader></B></FONT><BR><A NAME="1201355"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><A HREF="number.html#1200968"  >toExponential</A>, <A HREF="number.html#1201389"  >toPrecision</A>, <A HREF="number.html#1193464"  >toString</A></zparagraph></FONT><P><BR><P><A NAME="1201389"> </A><A NAME="toPrecision"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="4"><B><zrefhead2>toPrecision</zrefhead2></B></FONT><P><A NAME="1201406"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Returns a string representing the Number object to the specified precision.<BR><P><TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0">  <CAPTION></CAPTION><P><FONT FACE=""><B></B></FONT><BR><BR>  <TR>    <TD VALIGN=TOP><P><A NAME="1201392"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><I>Method of</I></ztabletext></FONT>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1201397"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><A HREF="number.html#1193137"  >Number</A></ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><P><A NAME="1201399"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><I>Implemented in</I></ztabletext></FONT>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1201401"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext>JavaScript 1.5</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><P><A NAME="1201403"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><I>ECMA version</I></ztabletext></FONT>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1201405"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext>ECMA-262, Edition 3</ztabletext></FONT>&nbsp;</TD>  </TR></TABLE><P> </zparagraph></FONT><P><BR><A NAME="1201407"> </A><A NAME="Syntax"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Syntax</zrefheader></B></FONT><BR><A NAME="1201408"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>toPrecision([<I>precision</I>])</zcodeline></FONT><P><BR><A NAME="1201409"> </A><A NAME="Parameters"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Parameters</zrefheader></B></FONT><BR><A NAME="1201415"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><BR><P><TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0">  <CAPTION></CAPTION><P><FONT FACE=""><B></B></FONT><BR><BR>  <TR>    <TD VALIGN=TOP><A NAME="1201412"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>precision</zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1201414"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext>An integer specifying the number of digits after the decimal point. </ztabletext></FONT>&nbsp;</TD>  </TR></TABLE><P></zparagraph></FONT><P><BR><A NAME="1201416"> </A><A NAME="Description"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Description</zrefheader></B></FONT><BR><A NAME="1201719"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">Number.prototype.toPrecision</FONT> method returns a string representing a Number object in fixed-point or exponential notation rounded to <FONT FACE="courier, couriernew, monospaced size"><I>precision</I></font> significant digits.</zparagraph></FONT><P><A NAME="1206997"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>If you use the <FONT FACE="courier, couriernew, monospaced size">toPrecision</FONT> method for a numeric literal and the numeric literal has no exponent and no decimal point, leave a space before the dot that precedes the method call to prevent the dot from being interpreted as a decimal point.</zparagraph></FONT><P><A NAME="1201723"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>If the <FONT FACE="courier, couriernew, monospaced size"><I>precision</I></font> argument is omitted, behaves as <FONT FACE="courier, couriernew, monospaced size">Number.prototype.toString</FONT>.</zparagraph></FONT><P><A NAME="1207425"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>If a number has more digits that requested by the <FONT FACE="courier, couriernew, monospaced size"><I>precision </I></font>parameter, the number is rounded to the nearest number represented by <FONT FACE="courier, couriernew, monospaced size"><I>precision </I></font>digits. See the discussion of rounding in the description of the <FONT FACE="courier, couriernew, monospaced size">toFixed</FONT> method on <A HREF="number.html#1207032"  >page&nbsp;129</A>, which also applies to <FONT FACE="courier, couriernew, monospaced size">toPrecision</FONT>.</zparagraph></FONT><P><BR><A NAME="1201927"> </A><A NAME="Examples"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Examples</zrefheader></B></FONT><BR><A NAME="1202066"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>var num=5.123456<br>alert("num.toPrecision() is " + num.toPrecision()) //displays 5.123456<br>alert("num.toPrecision(4) is " + num.toPrecision(4)) //displays 5.123<br>alert("num.toPrecision(2) is " + num.toPrecision(2)) //displays 5.1<br>alert("num.toPrecision(2) is " + num.toPrecision(1)) //displays 5<br>alert("num.toPrecision(2) is " + num.toPrecision(1)) //displays 5<br>alert("1250 .toPrecision() is " + 1250 .toPrecision(2))<br>//displays 1.3e+3<br>alert("1250 .toPreci

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -