0
I’m doing a search on MongoDB
and bringing the sum of some values. It correctly brings the values that are there, adding them up. The problem is when I need to take for example the value 1 and add with the value 2. I have as an example 2 numbers:
Valor 1: 980
Valor 2: 2.094
And the sum of that result is giving 982.094 which would be 3.074.
The code I use:
$queryReceitaMeta = $this->mongo_db->aggregate('metas_receita', $aggregate);
if(count($queryReceitaMeta['result']) > 0){
$rowQueryReceitaMeta = $queryReceitaMeta['result'][0];
foreach($meses as $mes){
$totalReceitaMeta += $rowQueryReceitaMeta['soma_'.$mes];
}
}
In this code he sums up the result of the whole year, being that he sums up each month (January-December).
When I take the "." (point) of the values, then it sums right. Using the function
str_replace
it works normally.
The
.
in the US and some other countries symbolizes the beginning of the decimal places, as well as the,
in Brazil. I think the behavior is correct. I suggest you check the location (locale) options of your database and PHP to see if they are aligned.– Leonardo Pessoa
I spent more time thinking about how it should be 3,074... The sum you want to make should be
0.980 + 2.094
, that would be 3,074. If you want 3,074 (three thousand and seventy-four) you should omit the.
, thus980+2094
. Add980.000 + 3.074
will give982.094
, which is what you’re doing.– Inkeliz