📄 ch02_04.htm
字号:
<HTML><HEAD><TITLE>Recipe 2.3. Rounding Floating-Point Numbers (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:30:04Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch02_01.htm"TITLE="2. Numbers"><LINKREL="prev"HREF="ch02_03.htm"TITLE="2.2. Comparing Floating-Point Numbers"><LINKREL="next"HREF="ch02_05.htm"TITLE="2.4. Converting Between Binary and Decimal"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_03.htm"TITLE="2.2. Comparing Floating-Point Numbers"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 2.2. Comparing Floating-Point Numbers"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch02_01.htm"TITLE="2. Numbers"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_05.htm"TITLE="2.4. Converting Between Binary and Decimal"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 2.4. Converting Between Binary and Decimal"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch02-17192">2.3. Rounding Floating-Point Numbers</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-233">Problem <ACLASS="indexterm"NAME="ch02-idx-1000008002-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008002-1"></A><ACLASS="indexterm"NAME="ch02-idx-1000008002-2"></A><ACLASS="indexterm"NAME="ch02-idx-1000008002-3"></A><ACLASS="indexterm"NAME="ch02-idx-1000008002-4"></A></A></H3><PCLASS="para">You want to round a floating-point value to a certain number of decimal places. This problem arises as a result of the same inaccuracies in representation that make testing for equality difficult (see <ACLASS="xref"HREF="ch02_03.htm"TITLE="Comparing Floating-Point Numbers">Recipe 2.2</A>), as well as in situations where you must reduce the precision of your answers for readability.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-239">Solution</A></H3><PCLASS="para">Use the Perl function <CODECLASS="literal">sprintf</CODE>, or <CODECLASS="literal">printf</CODE> if you're just trying to produce output:</P><PRECLASS="programlisting">$rounded = sprintf("%FORMATf", $unrounded);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-247">Discussion</A></H3><PCLASS="para">Rounding can seriously affect some algorithms, so the rounding method used should be specified precisely. In sensitive applications like financial computations and thermonuclear missiles, prudent programmers will implement their own rounding function instead of relying on the programming language's built-in logic, or lack thereof.</P><PCLASS="para">Usually, though, we can just use <CODECLASS="literal">sprintf</CODE>. The <CODECLASS="literal">f</CODE> format lets you specify a particular number of decimal places to round its argument to. Perl looks at the following digit, rounds up if it is 5 or greater, and rounds down otherwise.</P><PRECLASS="programlisting">$a = 0.255;$b = sprintf("%.2f", $a);print "Unrounded: $a\nRounded: $b\n";printf "Unrounded: $a\nRounded: %.2f\n", $a;<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Unrounded: 0.255</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Rounded: 0.26</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Unrounded: 0.255</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Rounded: 0.26</I></CODE></B></CODE></PRE><PCLASS="para">Three functions that may be useful if you want to round a floating-point value to an integral value are <CODECLASS="literal">int</CODE><ACLASS="indexterm"NAME="ch02-idx-1000008008-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008008-1"></A><ACLASS="indexterm"NAME="ch02-idx-1000008008-2"></A>, <CODECLASS="literal">ceil</CODE>, and <CODECLASS="literal">floor</CODE>. <CODECLASS="literal">int</CODE>, built into Perl, returns the integral portion of the floating-point number passed to it (<CODECLASS="literal">int</CODE> will use <CODECLASS="literal">$_</CODE> if it was called without an argument). The <ACLASS="indexterm"NAME="ch02-idx-1000008079-0"></A>POSIX module's <CODECLASS="literal">floor</CODE> and <CODECLASS="literal">ceil</CODE> functions round their argument down and up to the next integer, respectively.</P><PRECLASS="programlisting">use POSIX;print "number\tint\tfloor\tceil\n";@a = ( 3.3 , 3.5 , 3.7, -3.3 );foreach (@a) { printf( "%.1f\t%.1f\t%.1f\t%.1f\n", $_, int($_), floor($_), ceil($_) );}<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>number int floor ceil</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 3.3 3.0 3.0 4.0</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 3.5 3.0 3.0 4.0</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> 3.7 3.0 3.0 4.0</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>-3.3 -3.0 -4.0 -3.0</I></CODE></B></CODE></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-305">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">sprintf</CODE> and <CODECLASS="literal">int</CODE> functions in <EMCLASS="emphasis">perlfunc </EM>(1) and <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the <CODECLASS="literal">floor</CODE> and <CODECLASS="literal">ceil</CODE> entries in the documentation for the standard POSIX module (also in <ACLASS="olink"HREF="../prog/ch07_01.htm">Chapter 7</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>); we introduced the <CODECLASS="literal">sprintf</CODE> technique in <ACLASS="xref"HREF="ch02_03.htm"TITLE="Comparing Floating-Point Numbers">Recipe 2.2</A> <ACLASS="indexterm"NAME="ch02-idx-1000008004-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008004-1"></A><ACLASS="indexterm"NAME="ch02-idx-1000008004-2"></A><ACLASS="indexterm"NAME="ch02-idx-1000008004-3"></A><ACLASS="indexterm"NAME="ch02-idx-1000008004-4"></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_03.htm"TITLE="2.2. Comparing Floating-Point Numbers"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 2.2. Comparing Floating-Point Numbers"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_05.htm"TITLE="2.4. Converting Between Binary and Decimal"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 2.4. Converting Between Binary and Decimal"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">2.2. Comparing Floating-Point Numbers</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">2.4. Converting Between Binary and Decimal</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -