Result with incorrect formatting

Asked

Viewed 352 times

-3

I’m having the following difficulty:

The calculation is doing right!

R$ 0.01986 when the right one would be R$ 19.86

How do I format this result?
Code of the result:

<span style="font-size: 12px">Custo (/Sc):<b> 
<?php $g  = round ($c/$result->valorsc,5)?>
<?php echo $g?>
  • 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?

  • I’ll explain to you :

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

  • Before anything, please correct the code in the question.

  • done to friend change , help me please, is it formatting pq the calculation right ta :(

  • 1

    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.

  • What are the values of the variables $c and $result->valorsc? The problem is in them. Use var_dump($c, $result->valorsc); to take such values and inform us.

  • 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

  • @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

  • guilerme help me kkkk I’m still beginner , that ta very hard kkkkkk

  • @Fabiobarroso edited the answer: https://answall.com/a/245422/3635

  • 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

Show 7 more comments

2 answers

4

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.

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 calculating machine to perform a calculation. You would type 1390.76

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

In PHP the conversion from string to number depends on the format of the string, so PHP evaluates the format of the string and if it has no numerical value it will be converted to 0(zero). If it has a numerical value in his first position the value will be considered and if the value is not in the first position 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)

See in ideone the complete code executed 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

A 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, ',', '.');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.