0

I've a function wich sum some payments.

function get_payment() {

   return 1439.60 + 1439.60 + 1439.60;

}

$a = get_payment();

var_export($a);

//I GET: $a = 4318.7999999999993;

Now i'm trying to round to 2 decimal without success

$b = round($a, 2);

var_export($b);

//I GET: $b = 4318.8000000000002;

I'd like to have 4318.80 in FLOAT type to compare with others values.

I don't want to use number_format (to display result)

Giuseppe Lodi Rizzini
  • 1,045
  • 11
  • 33

3 Answers3

2

$b = number_format((float)$a, 2, '.', ''); This will make $b store as rounded $a

Asheeka K P
  • 408
  • 4
  • 13
0

I think the problem is var_export, which for some reason, ignores the 'round'

var_export($a);
// 4318.79999999999927240423858165740966796875

$b = round($a, 2);
var_export($b);
// 4318.8000000000001818989403545856475830078125

var_dump($b);
// float(4318.8)

echo $b;
//4318.8
Leo
  • 674
  • 5
  • 18
0

as you do not want to use number_format , you can try with bcdiv

$b=bcdiv($a, 1, 2);
echo $b;