want to display exactly 2 digits after floating point

floating-point, php

Solution

This does what I think you are asking for:

<?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

Problem

I want to convert floating value of 8 digits after floating point in to 2 digits after floating point .. Eg. $a = 2.200000 ==> 2.20 I am using round function of php. Problem with round is if my number is 2.200000 it converts number in 2.2 . I want output as 2.20 Can any one suggest the 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 out put like if my floating number is 2.2000000. then it should return me 2.20. but right now it is returning me 2.2

Original source