How to display two decimal places in MySQL/PHP?

WBOY
Release: 2023-09-02 08:42:01
forward
957 people have browsed it

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;
Copy after login

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

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

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>
Copy after login

Output

The following is a snapshot of the PHP code−

10.34
10.00
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!