Your error log level is probably not displaying Warnings and/or Notices, but if it were enabled it might display this notification in the middle of the page:
Notice: A non 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 has to be a number that the machine understand.
For example, if you do this:
<?php
$valor1 = '100foo';
$valor2 = '2';
$total = $valor1 / $valor2;
var_dump($total);
I am returned this:
PHP Notice: A non well formed numeric value encountered in C:\Users\guilherme\foo.php on line 6
int(50)
See that managed to divide 100 by 2 = int(50)
, because PHP tries to "parse" the string, but does something more complicated like:
<?php
$valor1 = '100.000,00';
$valor2 = '2';
$total = $valor1 / $valor2;
var_dump($total);
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 recognized the 100.000
, which is "practically" the same thing 100.0
, or be a guy float
, and split it up like this 100.0/2
= float(50)
That is to say, 100.000,00
for the machine (independent of any language) is not a number, it is just a string
, in other languages outside PHP (which tries to parse) is capable of occurring 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 prefer to use DECIMAL
and work with real numbers, because if your bank is returning something like this, 100.000,00
is because it’s definitely a kind of CHAR
that he uses, please don’t use something like:
ALTER TABLE `minha_tabela` MODIFY `minha_culuna` DECIMAL(8,2)
Before changing the values of each of the lines, 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 lines, for example 100.000,00
shall turn `100000.00, manually or with a script that traverses all rows of 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 it like this in your script:
round(reverse_number_format($c) / reverse_number_format($result->valorsc), 5);
Note: but like I said, this is a solution palliative, the recommended is to change its structure.
If the result is 0.01986 when it should be 19.86, the calculation doesn’t seem right to me. What exactly are you trying to do?
– Woss
I’ll explain to you :
– Fabio Barroso
the total amount above is $ 1.390,76 / 70 is equal R $ 19.86 the right result ta, the problem is q ta appearing R $ 0.01986 what you think may be wrong ?
– Fabio Barroso
Before anything, please correct the code in the question.
– Woss
done to friend change , help me please, is it formatting pq the calculation right ta :(
– Fabio Barroso
Fabio, you need to be clearer in what are the values of the variables, but already I say that probably the problem is in the formatting of the number. PHP uses the American format, recognizing the dot as decimal separator; i.e.,
1.390
, in PHP, is a comma 390, nay one thousand and 390, as expected.– Woss
What are the values of the variables
$c
and$result->valorsc
? The problem is in them. Usevar_dump($c, $result->valorsc);
to take such values and inform us.– Guilherme Nascimento
the variable $c = $result->valueTotal/$result->area_id the values come from the database here this result is R$ 1,390.76 where it will be divided by the value variable of the database that is R$ 70. and the result of 0.01986 where would be the correct R $ 19.86
– Fabio Barroso
@Fabiobarroso the problem is the format of the numbers, you are trying to calculate something that is not number, see: https://answall.com/a/230964/3635
– Guilherme Nascimento
guilerme help me kkkk I’m still beginner , that ta very hard kkkkkk
– Fabio Barroso
@Fabiobarroso edited the answer: https://answall.com/a/245422/3635
– Guilherme Nascimento
this occurs because php will take a part of this string q can be interpreted as number, ie 1.390,76/70 for php is 1.39/70 q will give 0.01986
– user60252