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

J P

Journeyman
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.
 

About this Thread

  • 1
    Replies
  • 290
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
842
Guests online
977
Total visitors
1,819

Forum statistics

Threads
2,274,618
Posts
28,957,091
Members
1,233,385
Latest member
pluszero121
Back
Top