# Math with decimal is not working

Page 1 of 1

## 10 Replies - 824 Views - Last Post: 29 January 2013 - 10:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=309297&amp;s=76bdf5b07087d6e10160b5af7963f661&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 el_pancho

Reputation: -1
• Posts: 88
• Joined: 26-April 12

# Math with decimal is not working

Posted 25 January 2013 - 11:49 AM

hi guys,

i have a function from Brazilian´s mail company, that calculates the shipping cost from a product,

```function calcula_frete(\$servico,\$CEPorigem,\$CEPdestino,\$peso,\$altura='3',\$largura='25',\$comprimento='30',\$valor='1.00')

// Using this example
\$price= return \$this->calcula_frete("40010",\$this->cep_origem,\$this->cep,\$soma);

```

using this \$price = R\$14,90, the product price is R\$100, but when i do
```\$total = \$price + \$product; // 14,90 + 100
echo \$total;

```

it shows 114 instead of 114,90.

i tried to do (100,00 + 14,90) , (100.00 + 14,90) but is returns the same thing, 114.

what could be happening?

This post has been edited by el_pancho: 25 January 2013 - 11:49 AM

Is This A Good Question/Topic? 0

## Replies To: Math with decimal is not working

### #2 el_pancho

Reputation: -1
• Posts: 88
• Joined: 26-April 12

## Re: Math with decimal is not working

Posted 25 January 2013 - 11:55 AM

Even if i use

```number_format(\$total, 2, ',','.')

```

it shows 114.00

### #3 Anthonidas

Reputation: 31
• Posts: 240
• Joined: 25-April 11

## Re: Math with decimal is not working

Posted 25 January 2013 - 12:01 PM

can you please show the whole concerning code? what you posted is not enough to see if you did a mistake...

### #4 el_pancho

Reputation: -1
• Posts: 88
• Joined: 26-April 12

## Re: Math with decimal is not working

Posted 25 January 2013 - 12:11 PM

Anthonidas, on 25 January 2013 - 12:01 PM, said:

can you please show the whole concerning code? what you posted is not enough to see if you did a mistake...

Sure

```// \$total, this is what i want
public function showCarrinhoTotal()
{

\$total = 0;
\$imposto = \$this->returnFrete(\$_SESSION['id']);
\$subtotal = \$this->showCarrinhoSubtotal();

\$total = \$imposto + \$subtotal;

return \$total;
//return number_format(\$total, 2, ',','.');
}

public function returnFrete(\$id)
{
\$qr = \$this->sql->qr("SELECT * FROM carrinhos WHERE usuarios_id = '".\$id."' AND vendas_id IS null");

if(!isEmpty(\$qr))
{
\$produtos = array();
\$n = 0;

while(\$ln = mysql_fetch_array(\$qr))
{

\$nr = \$this->sql->qr("SELECT * FROM produtos WHERE id = '".\$ln['produtos_id']."' ");

while(\$nn = mysql_fetch_array(\$nr))
{
}

\$n++;
}

\$i = 0;
\$soma = 0;
while(\$i < \$n)
{
foreach(\$produtos as \$key => \$value)
{
\$soma = \$soma + (\$value['qtde']*\$value['peso']);
}
\$i++;
}

return \$this->calcula_frete("40010",\$this->cep_origem,\$this->cep,\$soma);

}
else
{
return "0";
}
}

function calcula_frete(\$servico,\$CEPorigem,\$CEPdestino,\$peso,\$altura='3',\$largura='25',\$comprimento='30',\$valor='1.00'){
////////////////////////////////////////////////
// Código dos Serviços dos Correios
// 41106 PAC
// 40010 SEDEX
// 40045 SEDEX a Cobrar
// 40215 SEDEX 10
////////////////////////////////////////////////
// URL do WebService

ini_set("user_agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
ini_set("max_execution_time", 0);
ini_set("memory_limit", "10000M");

// Carrega o XML de Retorno
// Verifica se não há erros
if(\$xml->cServico->Erro == '0'){
return \$xml->cServico->Valor;
}else{
return 0;
}
}

```

This post has been edited by el_pancho: 25 January 2013 - 12:12 PM

### #5 Anthonidas

Reputation: 31
• Posts: 240
• Joined: 25-April 11

## Re: Math with decimal is not working

Posted 25 January 2013 - 12:47 PM

did you try to use (decimal) near your variable? or to manually set decimal values, without using all other functions, to see if it calculates right at least in your showCarrinhoTotal()?

### #6 el_pancho

Reputation: -1
• Posts: 88
• Joined: 26-April 12

## Re: Math with decimal is not working

Posted 25 January 2013 - 12:54 PM

It doensīt work but, if i vardump \$imposto it says:

```object(SimpleXMLElement)[12]
string '14,90' (length=5)

```

### #7 Dormilich

• 痛覚残留

Reputation: 3987
• Posts: 12,312
• Joined: 08-June 10

## Re: Math with decimal is not working

Posted 25 January 2013 - 01:27 PM

and there you have the problem. 14,90 is not a valid number. all programming languages (I know) use english notation, i.e. 14.90. since you add 10 (or 100) to the string "14,90" the string is parsed as a number, cutting off at the first invalid character, which is ,, hence leaving 14. you’ll have to parse that string back into the right format to get the number you expect.

if the numbers stay as simple as that, str_replace() should do, otherwise you can try sscanf().

This post has been edited by Dormilich: 25 January 2013 - 01:32 PM

### #8 andrewsw

• But the opposite, you said.

Reputation: 5520
• Posts: 21,776
• Joined: 12-December 12

## Re: Math with decimal is not working

Posted 25 January 2013 - 03:01 PM

As suggested, you might use str_replace() or sscanf() to convert the received values into numbers. After having performed calculations on the value, you might use number_format() (possibly money_format()) to return the result. But these will return the result as a string, so the same process will need to be adopted for any further calculations.

There is set_locale() although I don't know much about it, but I understand that it causes more problems than it cures, particularly wrt MySql databases.

This post has been edited by andrewsw: 25 January 2013 - 03:09 PM

### #9 Anthonidas

Reputation: 31
• Posts: 240
• Joined: 25-April 11

## Re: Math with decimal is not working

Posted 27 January 2013 - 02:30 AM

LOL you got a vote up, because it really causes more problems then it cures!!

Anyway, el_pancho, I suggest you, to get into using everytime the english format with dots instead of your local format. It is better for calculations, for databases, and so on... and at the end when you show up your values, you just change the displayed format.

### #10 el_pancho

Reputation: -1
• Posts: 88
• Joined: 26-April 12

## Re: Math with decimal is not working

Posted 27 January 2013 - 06:53 AM

Thank you everyone i ll try to solve this issue on monday and i ll post a feedback, thank you

### #11 el_pancho

Reputation: -1
• Posts: 88
• Joined: 26-April 12

## Re: Math with decimal is not working

Posted 29 January 2013 - 10:09 AM

hi guys,

i am sry for not posting a response yesterday, but it worked pretty well using str_replace to change ( , ) for ( . ), i already sent a feedback to the developīs department from brazilianīs mail company suggesting some changes.

Thank you. and if u guys want to come to Brazil, call me i ll buy the beer!