Home  >  Article  >  Backend Development  >  How to display two decimal places in MySQL/PHP?

How to display two decimal places in MySQL/PHP?

WBOY
WBOYforward
2023-09-02 08:41:11992browse

How to display two decimal places in MySQL/PHP?

To display two decimal places, use number_format(). Let's start by writing the same PHP code. We first declare two variables to be initialized-

$number1=10.3423;
$number2=10;

Now, use the number_format() function to display two decimals-

$result1=number_format ($number1, 2);
$result2=number_format ($number2, 2);

Example

The following is an example-

<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">&#39;<br>&#39;</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>

Output

The following is a snapshot of the PHP code−

10.34
10.00

The above is the detailed content of How to display two decimal places in MySQL/PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete