How to make an exact / precise division or subtraction in PHP

0

I'm having trouble making an exact calculation in PHP, please note.

$selic = 0,96;
$ipca  = 0,42;
bcscale(10);
$sub = bcsub($selic, $ipca);   //Retona: 0.54000000
// ou
$sub = $selic - $ipca;         //Retorna: 0.54

The exact result for this no Excel formula is: 0.54316901573592300

This is for subtraction, addition, division and multiplication, can anyone help me?

Editing
It was bad there, the error is in Excel, not with PHP. I'm not used to it at all. But thank you all.

    
asked by anonymous 12.04.2017 / 22:46

1 answer

3

Correcting your example, php will not interpret these "commas" have to be point, and have to be in quotation marks if it is to use string transformation, or without quotation marks if it is a numerical one, but equal will have to change commas by Score.

<?php
$selic = "0.96";
$ipca  = "0.42";
bcscale(10);
echo bcsub($selic, $ipca);   //Retona: 0.54000000
echo "<br>";// ou
echo ($selic - $ipca);         //Retorna: 0.54


$selic = "0.96013899947120500";
$ipca  = "0.41696998373528200";
bcscale(10);
echo bcsub($selic, $ipca,17);   //Retona: 0.54316901573592300
echo "<br>";// ou
echo ($selic - $ipca);         //Retorna: 0.54316901573592

Updated as comments

    
12.04.2017 / 22:55