﻿﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>帮助信息-动天数据 &#187; php四舍五入</title>
	<atom:link href="http://www.583idc.com/news/?feed=rss2&#038;tag=php%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5" rel="self" type="application/rss+xml" />
	<link>http://www.583idc.com/news</link>
	<description>提供虚拟主机帮助信息</description>
	<lastBuildDate>Mon, 30 Oct 2023 02:58:59 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>php四舍五入 js四舍五入方法</title>
		<link>http://www.583idc.com/news/?p=1820</link>
		<comments>http://www.583idc.com/news/?p=1820#comments</comments>
		<pubDate>Sat, 16 Dec 2017 13:32:39 +0000</pubDate>
		<dc:creator><![CDATA[dthost]]></dc:creator>
				<category><![CDATA[未分类]]></category>
		<category><![CDATA[JS四舍五入]]></category>
		<category><![CDATA[php四舍五入]]></category>

		<guid isPermaLink="false">http://www.583idc.com/news/?p=1820</guid>
		<description><![CDATA[四舍五入是一种精确度的计数保留法，与其他方法本质相同。特殊之处在于，采用四舍五入，能使被保留部分的与实际值差值 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>四舍五入是一种精确度的计数保留法，与其他方法本质相同。特殊之处在于，采用四舍五入，能使被保留部分的与实际值差值不超过最后一位数量级的二分之一：假如0～9等概率出现的话，对大量的被保留数据，这种保留法的误差总和是最小的，因此四舍五入法也是最基本的保留法。</p>
<p><img title="四舍五入" src="http://a4.att.hudong.com/73/93/20300000432684141618934744840_s.jpg" alt="四舍五入" />、</p>
<p>例子一：例如π被四舍五入，保留下3.14。但是，有的时候不可以用四舍五入的方法，而要用“进一法”和“去尾法”。例如，288个学生春游，45人一辆大巴，算下来是6.4辆大巴，但是必须进一才可以不让人多出来，不让车少，因为车的数量不能为小数，所以需要7辆大巴。再例如，1016升汽油，要给汽车加油，20升一辆，平均可加50.8辆，但是必须去尾才可以不让车多出来，让油少，因为车的数量不能为小数，所以可以给50辆汽车加油。<br data-filtered="filtered" />注：数量级：即数字所在位置权值，如3.14159这个数，3的数量级为1(10^0），9的数量级为0.00001(10^-5）。</p>
<p>本文介绍php四舍五入函数使用方法，php 数据处理之<strong>取整</strong>,<strong>四舍五入</strong>分为四个部分，<strong>php四舍五入函数使用</strong>分别如下。<br />
1.丢弃小数部分,保留整数部分</p>
<table class="syntaxhighlighter  php" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php functions">intval</code><code class="php plain">(7/2)</code></div>
</div>
</td>
</tr>
</tbody>
</table>
<p class="sycode">2.向上取整,有小数就整数部分加1</p>
<table class="syntaxhighlighter  php" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php functions">ceil</code><code class="php plain">(7/2)</code></div>
</div>
</td>
</tr>
</tbody>
</table>
<p class="sycode">3, 对浮点数进行四舍五入</p>
<table class="syntaxhighlighter  php" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php functions">round</code><code class="php plain">(7/2)</code></div>
</div>
</td>
</tr>
</tbody>
</table>
<p class="sycode">如果<strong>php四舍五入要保留两位小数</strong>，后来参数根保留小数位数即可。</p>
<table class="syntaxhighlighter  html" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="html plain">$number = 1234.5678;</code></div>
<div class="line number2 index1 alt1"><code class="html plain">echo round($number ,2); //1234.57</code></div>
</div>
</td>
</tr>
</tbody>
</table>
<p class="sycode">4,向下取整</p>
<table class="syntaxhighlighter  php" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php functions">floor</code><code class="php plain">(7/2）</code></div>
</div>
</td>
</tr>
</tbody>
</table>
<p>PHP取整数函数常用的四种方法,下面收集了四个函数； 经常用到取整的函数，今天小小的总结一下！其实很简单，就是几个函数而已～～主要是：ceil，floor，round，intval<br />
<strong>一、ceil — 进一法取整 </strong>说明 float ceil ( float value ) 返回不小于 value 的下一个整数，value 如果有小数部分则进一位。ceil() 返回的类型仍然是 float，因为 float 值的范围通常比 integer 要大。 例子 1. ceil() 例子 &lt; ?php echo ceil(4.3); // 5 echo ceil(9.999); // 10 ?&gt;<br />
<strong>二、floor — 舍去法取整 </strong>说明 float floor ( float value ) 返回不大于 value 的下一个整数，将 value 的小数部分舍去取整。floor() 返回的类型仍然是 float，因为 float 值的范围通常比 integer 要大。 例子 1. floor() 例子 &lt; ?php echo floor(4.3); // 4 echo floor(9.999); // 9 ?&gt;<br />
<strong>三、round — 对浮点数进行四舍五入 </strong>说明 float round ( float val [, int precision] ) 返回将 val 根据指定精度 precision（十进制小数点后数字的数目）进行四舍五入的结果。precision 也可以是负数或零（默认值）。 例子 1. round() 例子 &lt; ?php echo round(3.4); // 3 echo round(3.5); // 4 echo round(3.6); // 4 echo round(3.6, 0); // 4 echo round(1.95583, 2); // 1.96 echo round(1241757, -3); // 1242000 echo round(5.045, 2); // 5.05 echo round(5.055, 2); // 5.06 ?&gt;<br />
<strong>四、intval—对变数转成整数型态 </strong>例子intval() &lt; ?php echo intval(4.3); //4 echo intval(4.6); // 4 ?&gt;</p>
<p><b>php</b> 教程 <strong><b>php</b>除法 </strong><b>余数</b>处理方法[四舍五入]和[有<b>余数</b>则<b>进</b>1]两种处理方式-<b>php</b>基础教程</p>
<p>echo round(3.4);         // 3</p>
<p>echo round(3.5);         // 4<br data-filtered="filtered" />echo round(3.6);         // 4<br data-filtered="filtered" />echo round(3.6, 0);      // 4<br data-filtered="filtered" />echo round(1.95583, 2);  // 1.96<br data-filtered="filtered" />echo round(1241757, -3); // 1242000<br data-filtered="filtered" />echo round(5.045, 2);    // 5.05<br data-filtered="filtered" />echo round(5.055, 2);    // 5.06</p>
<p>echo ceil(4.3);    // 5<br data-filtered="filtered" />echo ceil(9.999);  // 10<br data-filtered="filtered" />echo ceil(-3.14);  // -3</p>
<p>方法一：<br data-filtered="filtered" /> 在js中四舍五入的函数 tofixed(n) , n为要保留的小数位数。<br data-filtered="filtered" /> n为0~20，当n超过20的时候，js会出错。<br data-filtered="filtered" /> var d=10.005;<br data-filtered="filtered" />  var f=d.tofixed(2);<br data-filtered="filtered" />  alert(f);<br data-filtered="filtered" />  bug:<br data-filtered="filtered" />  如果小数点前和要截取的前一位都是0时，不会按常理截取。<br data-filtered="filtered" />  var h=0.07<br data-filtered="filtered" />  h.tofixed(1)的值为0.0<br data-filtered="filtered" />  如果要修改这个缺陷，可以把js中的number类型的tofixed方法重写。<br data-filtered="filtered" />例如：<br data-filtered="filtered" /></p>
<div class="codetitle"><span style="text-decoration: underline;">复制代码</span>代码示例:</div>
<div id="c_W7IsF3uk" class="codebody">number.prototype.tofixed = function(d)<br data-filtered="filtered" />  {<br data-filtered="filtered" />      var s=this+"";if(!d)d=0;<br data-filtered="filtered" />      if(s.indexof(".")==-1)s+=".";s+=new array(d+1).join("0");<br data-filtered="filtered" />      if (new regexp("^(-|\\+)?(\\d+(\\.\\d{0,"+ (d+1) +"})?)\\d*$").test(s))<br data-filtered="filtered" />      {<br data-filtered="filtered" />  var s="0"+ regexp.$2, pm=regexp.$1, a=regexp.$3.length, b=true;<br data-filtered="filtered" />  if (a==d+2){a=s.match(/\d/g); if (parseint(a[a.length-1])&gt;4)<br data-filtered="filtered" />  {<br data-filtered="filtered" />      for(var i=a.length-2; i&gt;=0; i--) {a[i] = parseint(a[i])+1;<br data-filtered="filtered" />      if(a[i]==10){a[i]=0; b=i!=1;} else break;}<br data-filtered="filtered" />  }<br data-filtered="filtered" />  s=a.join("").replace(new regexp("(\\d+)(\\d{"+d+"})\\d$"),"$1.$2");<br data-filtered="filtered" />      }if(b)s=s.substr(1);return (pm+s).replace(/\.$/, "");} return this+"";<br data-filtered="filtered" />};   <br data-filtered="filtered" /></div>
<p>这样就可以修复number类型的tofixed方法。</p>
<p>方法二： <br data-filtered="filtered" />之前一直在用这个js自带的tofixed函数来进行四舍五入的操作，可是，在实际使用过程中却遇到了问题。</p>
<p>比如<br data-filtered="filtered" /></p>
<div class="codetitle"><span style="text-decoration: underline;">复制代码</span>代码示例:</div>
<div id="c_rhQGmiLZ" class="codebody">var money=0.00542;//0.006;   <br data-filtered="filtered" />alert(number(money).tofixed(2));   <br data-filtered="filtered" />//0.00  <br data-filtered="filtered" /></div>
<p>以上结果是错误的，下面的方法通过巧妙的使用math.round函数，完全可以解决数值很小时的精度问题。<br data-filtered="filtered" /></p>
<div class="codetitle"><span style="text-decoration: underline;">复制代码</span>代码示例:</div>
<div id="c_A7EBi68s" class="codebody">var money=0.00542;//0.006;   <br data-filtered="filtered" />alert(number(money).tofixed(2));   <br data-filtered="filtered" />  <br data-filtered="filtered" />function round2(number,fractiondigits){   <br data-filtered="filtered" />    with(math){   <br data-filtered="filtered" />        return round(number*pow(10,fractiondigits))/pow(10,fractiondigits);   <br data-filtered="filtered" />    }   <br data-filtered="filtered" />}   <br data-filtered="filtered" />alert(round2(money,2));//0.01</div>
<p>round 方法<br data-filtered="filtered" />返回与给出的数值表达式最接近的整数。</p>
<p>math.round(number)<br data-filtered="filtered" />必选项 number 参数是要舍入到最接近整数的值。</p>
<p>说明<br data-filtered="filtered" />如果 number 的小数部分大于等于 0.5，返回值是大于 number 的最小整数。<br data-filtered="filtered" />否则，round 返回小于等于 number 的最大整数。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="show_box">
<p>wps表格四舍五入怎么取消？当我们在wps表格上输入的一些数据之后，被wps表格给四舍五入了。如果不需要四舍五入该怎么办呢？下面为大家介绍一下wps表格四舍五入取消方法，一起来看看吧！</p>
</div>
<div class="show_box">
<p>        <strong>wps表格四舍五入取消方法</strong></p>
<p>1、首先我们打开wps表格，然后右击单元格===设置单元格格式===选择“常规”确定</p>
<p><center><img src="http://img0.pconline.com.cn/pconline/1711/10/10288501_20150429030431333_thumb.jpg" alt="" /></p>
<p></center><center><img src="http://img0.pconline.com.cn/pconline/1711/10/10288501_20150429030431339_thumb.jpg" alt="" /></p>
<p></center>　　2、然后我们可以点击“开始”菜单，然后再我们工具栏上有这么一个按钮“增加小数位数”</p>
<p><center><img src="http://img0.pconline.com.cn/pconline/1711/10/10288501_20150429030431116_thumb.jpg" alt="" /></p>
<p>&nbsp;</p>
<p></center></div>
]]></content:encoded>
			<wfw:commentRss>http://www.583idc.com/news/?feed=rss2&#038;p=1820</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
