English 中文(简体)
基本的 PHP表单帮助(货币显示)
原标题:
  • 时间:2008-12-06 01:31:49
  •  标签:

你们昨天很有帮助。不过我仍然有点困惑。

我想把右侧栏的数字四舍五入到最接近的整数美元。

将此翻译成中文:http://www.nextadvisor.com/voip_services/voip_calculator.php?monthlybill=50&Submit=Submit http://www.nextadvisor.com/voip_services/voip_calculator.php?monthlybill=50&Submit=Submit

这个表格的代码看起来像这样:

我想将$offer[1,2,3,4,5,6,7]calcsavingsann四舍五入,我该怎么做?

 <table width="100%;" border="0" cellspacing="0" cellpadding="0"class="credit_table2" >

    <tr class="credit_table2_brd">
     <td class="credit_table2_brd_lbl" width="100px;">Services:</td>
<td class="credit_table2_brd_lbl" width="120px;">Our Ratings:</td>
<td class="credit_table2_brd_lbl" width="155px;">Monthly VoIP Bill:</td>
<td class="credit_table2_brd_lbl" width="155px;">Annual Savings:</td>

   </tr>  

   <?php

 $offer1price="24.99";
 $offer2price="20.00";
 $offer3price="21.95";
 $offer4price="23.95";
 $offer5price="19.95";
 $offer6price="23.97";
 $offer7price="24.99";

 $offer1calcsavings= $monthlybill - $offer1price;
 $offer2calcsavings= $monthlybill - $offer2price;
 $offer3calcsavings= $monthlybill - $offer3price;
 $offer4calcsavings= $monthlybill - $offer4price;
 $offer5calcsavings= $monthlybill - $offer5price;
 $offer6calcsavings= $monthlybill - $offer6price;
 $offer7calcsavings= $monthlybill - $offer7price;

 $monthybill="monthlybill";

 $offer1calcsavingsann= $offer1calcsavings * 12;
 $offer2calcsavingsann= $offer2calcsavings * 12;
 $offer3calcsavingsann= $offer3calcsavings * 12;
 $offer4calcsavingsann= $offer4calcsavings * 12;
 $offer5calcsavingsann= $offer5calcsavings * 12;
 $offer6calcsavingsann= $offer6calcsavings * 12;
 $offer7calcsavingsann= $offer7calcsavings * 12;

   $re=1;
   $offer = offer .$re. name ;
 $offername= ${$offer};
   while($offername!=""){
$offerlo = offer .$re. logo ;
$offerlogo=${$offerlo};
$offerli = offer .$re. link ;
$offerlink=${$offerli};
$offeran = offer .$re. anchor ;
$offeranchor=${$offeran};
$offerst = offer .$re. star1 ;
$offerstar=${$offerst};
$offerbot= offer .$re. bottomline ;
$offerbottomline=${$offerbot};
$offerca = offer .$re. calcsavings ;
$offercalcsavings=${$offerca};
    $offerpr = offer .$re. price ;
$offerprice=${$offerpr};
$offersavann = offer .$re. calcsavingsann ;
$offercalcsavingsann=${$offersavann};

echo  <tr >
     <td >
 <a href=" .$offerlink. " target="blank"><img src="http://www.nextadvisor.com .$offerlogo. " alt=" .$offername. " />
 </a>
 </td>
<td ><span class="rating_text">Rating:</span>
 <span class="star_rating1">
 <img src="http://www.nextadvisor.com .$offerstar. " alt="" />
 </span>
 <br />
  <div style="margin-top:5px; color:#0000FF;">
 <a href=" .$offerlink. " target="blank">Go to Site</a>
 <span style="margin:0px 7px 0px 7px;">|</span><a href=" .$offeranchor. ">Review</a>     

 </div> </td>
       <td >$ .$offerprice. </td>
     <td >$ .$offercalcsavingsann. </td>


   </tr> ;
   $re=$re+1;
   $offer = offer .$re. name ;
 $offername= ${$offer};

   }



   ?>



 </table>
最佳回答

您想将金额四舍五入/舍去小数/保留整数到最接近的一美元吗?

这里是您可以使用的一些建议功能:

四舍五入

round(舍入)

地板函数

向上取整

格式化/截断

sprintf

问题回答

Grepseadawk的回答非常好,我要补充的是,你可以使用number_format来输出$336.60,而不是仅仅显示$336.6。

我知道这不是你的问题,但看了链接,我想这可能对你有用。

编辑 - 感谢安迪建议使用 money_format

money_format() 是一个函数,返回格式化数字的字符串值。您可以控制格式和数字。一个简单的例子,如果您的值存储在变量$myNumber中,您可以将结果合并到给定表格的数据单元格中,例如:

<?php echo ("<td>".money_format( %n ,$myNumber)."</td>"); ?>

And you would need to do this for every value, e.g. via a for loop if you had all your values in an array. The n here is one of the formatting options - there are several. A good place to look would be on the PHP web page at http://au2.php.net/manual/en/function.money-format.php Hope this helps.

我似乎无法正确使用。我使用echo的方法是:

echo  <tr >

  <td ><a href=" .$offerlink. " target="blank"><img src="http://www.nextadvisor.com .$offerlogo. " alt=" .$offername. " /></a></td>

 <td ><span class="rating_text">Rating:</span><span class="star_rating1"><img src="http://www.nextadvisor.com .$offerstar. " alt="" /></span><br />

  <div style="margin-top:5px; color:#0000FF;"><a href=" .$offerlink. " target="blank">Go to Site</a><span style="margin:0px 7px 0px 7px;">|</span><a href=" .$offeranchor. ">Review</a></div>   </td>

  <td >$ .$offerprice. </td>

<td >$ .$offercalcsavingsann. </td>


   </tr> ;

我把“设置地区”放在那里

 "<?php" 

我不明白如何写它,而且我用的每种方法都返回错误。





相关问题
热门标签