10 Replies - 650 Views - Last Post: 29 January 2013 - 10:09 AM Rate Topic: -----

#1 el_pancho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 225
  • 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...
Was This Post Helpful? 0
  • +
  • -

#4 el_pancho  Icon User is offline

  • D.I.C Head

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

Re: Math with decimal is not working

Posted 25 January 2013 - 12:11 PM

View PostAnthonidas, 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();
							$dados = array();
							$n = 0;
							
							while($ln = mysql_fetch_array($qr))
								{
									$dados['produtos_id'] = $ln['produtos_id'];
									$dados['qtde'] = $ln['qtde'];
									
									$nr = $this->sql->qr("SELECT * FROM produtos WHERE id = '".$ln['produtos_id']."' ");
									
									while($nn = mysql_fetch_array($nr))
										{
											$dados['peso'] = $nn['peso'];
										}
									
									$produtos[$n] = $dados;
									$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");
				
				$correios = "http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?nCdEmpresa=&sDsSenha=&sCepOrigem=".$CEPorigem."&sCepDestino=".$CEPdestino."&nVlPeso=".$peso."&nCdFormato=1&nVlComprimento=".$comprimento."&nVlAltura=".$altura."&nVlLargura=".$largura."&sCdMaoPropria=n&nVlValorDeclarado=".$valor."&sCdAvisoRecebimento=n&nCdServico=".$servico."&nVlDiametro=0&StrRetorno=xml";
				// Carrega o XML de Retorno
				$xml = simplexml_load_file($correios);
				// 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

Was This Post Helpful? 0
  • +
  • -

#5 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 225
  • 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()?
Was This Post Helpful? 1
  • +
  • -

#6 el_pancho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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)


Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,444
  • 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

Was This Post Helpful? 2
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,590
  • 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

Was This Post Helpful? 2
  • +
  • -

#9 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 225
  • 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!! :D

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.
Was This Post Helpful? 1
  • +
  • -

#10 el_pancho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#11 el_pancho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1