Pour afficher deux décimales, utilisez number_format(). Commençons par écrire le même code PHP. Nous déclarons d'abord deux variables à initialiser -
$number1=10.3423; $number2=10;
Maintenant, affichons deux décimales en utilisant la fonction number_format() −
$result1=number_format ($number1, 2); $result2=number_format ($number2, 2);
Voici l'exemple −
<span class="pun"><?</span><span class="pln">php</span> <span class="pln"> $number1</span><span class="pun">=</span><span class="lit">10.3423</span><span class="pun">;</span> <span class="pln"> $number2</span><span class="pun">=</span><span class="lit">10</span><span class="pun">;</span> <span class="pln"> $result1</span><span class="pun">=</span><span class="pln">number_format </span><span class="pun">(</span><span class="pln">$number1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">);</span> <span class="pln"> $result2</span><span class="pun">=</span><span class="pln">number_format </span><span class="pun">(</span><span class="pln">$number2</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">);</span> <span class="pln"> echo </span><span class="pun">(</span><span class="pln">$result1</span><span class="pun">);</span> <span class="pln"> echo </span><span class="str">'<br>'</span><span class="pun">;</span> <span class="pln"> echo </span><span class="pun">(</span><span class="pln">$result2</span><span class="pun">);</span> <span class="pun">?></span>
Voici l'instantané du code PHP −
10.34 10.00
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!