Domanda

Ieri siete stati molto utili ieri. Sono comunque un po 'confuso qui.

Voglio fare in modo che i numeri nella colonna più a destra siano arrotondati al dollaro più vicino:

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

il codice per la tabella è simile al seguente:

Voglio che $ offer [1,2,3,4,5,6,7] calcsavingsann sia arrotondato, come posso farlo?

 <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>
È stato utile?

Soluzione

Vuoi arrotondare per eccesso / per difetto / troncato al dollaro più vicino?

Ecco alcune funzioni suggerite che puoi usare:

Arrotondamento

round

floor

ceil

Formattazione / troncando

sprintf

Altri suggerimenti

La risposta di Grepsedawk è buona; l'unica cosa che aggiungerei è che invece di visualizzare $ 336,6, ad esempio, potresti usare number_format per produrre $ 336,60 .

(So che questa non era la tua domanda, ma guardando il link, ho pensato che potesse esserti utile.)

Modifica: grazie ad Andy per aver suggerito invece money_format .

money_format () è una funzione che restituisce un valore stringa di un numero formattato. Hai il controllo sulla formattazione e, ovviamente, sul tuo numero. Un semplice esempio, se hai il tuo valore nella variabile $ myNumber , potresti incorporare il risultato in una data cella della tabella in questo modo;

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

E dovresti farlo per ogni valore, ad es. tramite un ciclo for se avevi tutti i tuoi valori in un array. Il n qui è una delle opzioni di formattazione: ce ne sono diverse. Un buon posto dove guardare sarebbe sulla pagina web di PHP a http: / /au2.php.net/manual/en/function.money-format.php Spero che questo aiuti.

Non riesco a capire come utilizzarlo correttamente. Il modo in cui sto usando l'eco è

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 >

Ho inserito " set locale " dove

 "<?php" 

è. Non capisco come scriverlo e ogni modo che faccio restituisce solo un errore.

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

Ho inserito " set locale " dove

<*>

è. Non capisco come scriverlo e ogni modo che faccio restituisce solo un errore.

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

Ho inserito " set locale " dove

<*>

è. Non capisco come scriverlo e ogni modo che faccio restituisce solo un errore.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top