Pregunta

Ustedes fueron muy útiles ayer. Aunque todavía estoy un poco confundido aquí.

Quiero hacerlo para que los números en la columna de la derecha se redondeen al dólar más cercano:

http://www.nextadvisor.com/voip_services/voip_calculator .php? mensualbill = 50 & amp; Submit = Submit

el código de la tabla se ve así:

Quiero que la oferta de $ [1,2,3,4,5,6,7] calcsavingsann se redondee, ¿cómo puedo hacer esto?

 <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>
¿Fue útil?

Solución

¿Desea redondear hacia arriba / abajo / truncado al dólar más cercano?

Estas son algunas funciones sugeridas que puede usar:

Rounding

round

floor

ceil

Formateo / Truncamiento

sprintf

Otros consejos

La respuesta de Grepsedawk es buena; lo único que agregaría es que, en lugar de mostrar $ 336.6, por ejemplo, podría usar number_format para generar $ 336.60 .

(Sé que esta no era tu pregunta, pero mirando el enlace, pensé que podría ser útil para ti).

Editar - Gracias a Andy por sugerir money_format en su lugar.

money_format () es una función que devuelve un valor de cadena de un número formateado. Usted tiene control sobre el formato y, obviamente, su número. Un ejemplo simple, si tiene su valor en la variable $ myNumber , podría incorporar el resultado en la celda de datos de una tabla dada de esta manera;

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

Y necesitaría hacer esto para cada valor, p. a través de un bucle for si tenía todos sus valores en una matriz. El n aquí es una de las opciones de formato: hay varias. Un buen lugar para buscar sería en la página web de PHP en http: / /au2.php.net/manual/en/function.money-format.php Espero que esto ayude.

Parece que no puedo obtener el uso correcto. La forma en que estoy usando echo es

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 >

Puse el " set locale " donde el

 "<?php" 

es. No entiendo cómo escribirlo, y cada forma en que lo hago solo devuelve un error.

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

Puse el " set locale " donde el

<*>

es. No entiendo cómo escribirlo, y cada forma en que lo hago solo devuelve un error.

.$offercalcsavingsann.'</td> </tr>';

Puse el " set locale " donde el

<*>

es. No entiendo cómo escribirlo, y cada forma en que lo hago solo devuelve un error.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top