Incorrect formatting result [closed]

-2

I have the following difficulty:

The calculation is right!

R$ 0.01986 when correct would be R$ 19.86

How do I format this result? Result Code:

<span style="font-size: 12px">Custo (/Sc):<b> 
<?php $g  = round ($c/$result->valorsc,5)?>
<?php echo $g?>
    
asked by anonymous 11.10.2017 / 18:52

2 answers

4

Probably your error log level is not displaying Warnings and / or Notices, but if it were enabled maybe it would display this notification in the middle of the page:

  

Notice: A well-formed numeric value encountered in ... on line 34

You have to understand that a thing is a number, even if it is a string, its format must be a number that the machine understands .

For example, if you do this:

<?php

$valor1 = '100foo';
$valor2 = '2';

$total = $valor1 / $valor2;

var_dump($total);

This is returned to me:

PHP Notice:  A non well formed numeric value encountered in C:\Users\guilherme\foo.php on line 6
int(50)

See that you've even managed to split 100 by 2 = int(50) , this is because PHP tries to parse the string, but if it does something more complicated like:

<?php

$valor1 = '100.000,00';
$valor2 = '2';

$total = $valor1 / $valor2;

var_dump($total);

You will return this:

PHP Notice:  A non well formed numeric value encountered in C:\Users\guilherme\Desktop\debug.php on line 7
float(50)

The result that PHP tried to parse and displayed, but only acknowledged 100.000 , which is "pretty much the same 100.0 , ie a type float , and split it like this 100.0/2 = float(50)

That is, 100.000,00 to the machine (independent of any language) is not a number, it is just string , in other languages outside PHP (which tries to parse ) is able to occur exception or fatal errors when trying something like "100.000,00"/5

How to turn money format into a number?

First I really recommend that instead of using VARCHAR or CHAR in your bank you'd prefer to use DECIMAL and work with real numbers, because if your bank is returning something like this 100.000,00 it's because of certainty is a type of CHAR that it uses, please do not use something like:

ALTER TABLE 'minha_tabela' MODIFY 'minha_culuna' DECIMAL(8,2)

Before changing the values of each line, if you do this without changing you can be sure that it will break all the data, which will be impossible to reverse, first convert the existing rows, for example 100.000,00 should be '100000.00 , manually or with a script that traverses all rows in the table.

Now if you really want to insist on using CHAR ( VARCHAR or TEXT ) then palliatively you can try using function like this:

function reverse_number_format($str)
{
     if (is_numeric($str)) {
        return $str;
     } elseif (preg_match('#^(.*?)[.,\s](\d{2})$#', $str, $out)) {
        return preg_replace('#\D#', '', $out[1]) . '.' . $out[2];
     }

     return false;
}

And then use that in your script:

round(reverse_number_format($c) / reverse_number_format($result->valorsc), 5);
  

Note: But as I said, this is a palliative solution , it is recommended to change its structure.

    
11.10.2017 / 19:23
0

1,390,76 is not a number that can be used in mathematical calculations. You do not type this into your physical or virtual calculator to perform a calculation. You would type 1390.76

In programming also the same rule applies, the numbers must be of integer type or, integer separated by dot followed by fractional part, parteinteira.partefracionaria or for example of type 271e+139

In PHP the conversion of string to number depends on the format of the string, so PHP evaluates the format of the string and if it does not have any numerical value will be converted to 0 (zero). If you have numeric in your first position the value will be considered and if the value is not in the first position it will be disregarded.

Examples:

$string = (int) 'Muitas casas';

var_dump( $string ); // int(0)

$string2 = (int) '10 casas aproximadamente';

var_dump( $string2 ); // int(10)

$string3 = (int) 'Exatamente 10 casas';

var_dump( $string3 ); // int(0)

View in the ideon the complete code below

Wrong

$numerador = '1.390,76';
var_dump( $numerador ); //string(8) "1.390,76"

$divisor = '70'; //string(2) "70"
var_dump( $divisor );

echo ($numerador /$divisor); // 1.39/70

One solution

//tratando string para formato válido para calculos matematicos
$numerador = '1.390,76';

//retira os pontos (.)
$numerador=str_replace(".","",$numerador);

//transforma virgula (,) em ponto (.)
$numerador=str_replace(",",".",$numerador);

var_dump( $numerador ); string(7) "1390.76" 

$divisao = ($numerador/$divisor);

//formatando para 2 casas decimais
echo number_format($divisao, 2, ',', '.');
    
11.10.2017 / 23:48