What's new

How to show 3 decimals only and do not round off in PHP.

J P

Journeyman
Joined
Jun 1, 2023
Posts
34
Reaction
0
Points
24
PHP:
                            <tr>
                                <th class="p-1 text-right" colspan="5">Advance Payment</th>
                                <th class="p-1 text-right" colspan="">$</th>
                                <th class="p-1 text-left" id="discount_amount"><?php echo isset($discount_amount) ? $formatted = number_format($discount_amount, 2, '.', ',') : 0 ?></th>
                            <tr>
 
In the code snippet you provided, you are displaying the $discount_amount variable in a table cell with two decimal places using the number_format function. If you want to display the value with three decimal places without rounding off, you can update the number_format function as follows:

PHP:
<th class="p-1 text-left" id="discount_amount"><?php echo isset($discount_amount) ? number_format($discount_amount, 3, '.', '') : '0.000' ?></th>

By changing the second parameter of the number_format function to 3, you can display the value with three decimal places. Additionally, setting an empty string '' as the decimal separator and providing '0.000' as the default value will ensure that the number is displayed with three decimals even if $discount_amount is not set.

This modification will show the discount_amount with exactly three decimal places and avoid rounding off the value.
 

Similar threads

Back
Top