Want to display exactly 2 digits after floating point

I want to convert a floating point value of 8 digits after floating point to 2 digits after floating point.

Eg. $ a = 2.200000 ==> 2.20

I use the round php function. The problem with the round - if my number is 2.200000, it will convert the number to 2.2. I want the result to be 2.20

Can anyone suggest a possible way?

Actual code

$price = sprintf ("%.2f", round(($opt->price_value + ($opt->price_value * $this->row->prices[0]->taxes[0]->tax_rate)), 2));

I want it to be like if my floating number is 2.2000000. then he should return me 2.20. but now he brings me back 2.2

+5
source share
3 answers

This does what I think you are asking:

<?php

$a = 2.20032324;
$f = sprintf ("%.2f", $a);
echo "$a rounded to 2 decimal places is '$f'\n";

$a = 2.2000000;
$f = sprintf ("%.2f", $a);
echo "$a rounded to 2 decimal places is '$f'\n";

results:

[wally@lenovoR61 ~]$ php t.php
2.20032324 rounded to 2 decimal places is '2.20'
2.2 rounded to 2 decimal places is '2.20'

I added two test cases

+17
source

, .

       $a = 2.200000;
       echo number_format((float)$a,2,'.','');
-1

$val = round($a * 100)/100;

-1

All Articles