RE: [SMARTY] problem displaying float

From: Date: Fri, 17 Nov 2006 14:10:12 +0000
Subject: RE: [SMARTY] problem displaying float
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-13572@lists.php.net to get a copy of this message
Mark, > George Pitcher wrote: > >>> The following code: > >>> > >>> $fee = 160.44; > >>> $fee2 = $fee + 0; // I have a reason for adding zero; > >>> echo $fee."<br>"; > >>> echo $fee2."<br>"; > >>> settype($fee, "float"); > >>> echo gettype($fee); > >>> > > gettype($fee) before settype() gave me 'object'. > > > > It shouldn't have! > > Is this a complete code example, in PHP (ie not in Smarty)? > > On my server the following PHP code: > <?php > $fee = 160.44; > $fee2 = $fee + 0; // I have a reason for adding zero; > echo $fee."<br>"; > echo $fee2."<br>"; > echo gettype($fee)."<br>"; > echo gettype($fee2)."<br>"; > ?> > gives: > 160.44 > 160.44 > double > double > > .. as I would have expected. > > I think you need to resolve this first, but I'm almost certain the > problem is that the code example you've posted is not identical to the > code you're trying. > > > I'm doing all this in the php and sending it to the template using echo. > > > > I'm not sure what you mean by "sending it to the template using echo". > It sounds like you're sending it to the browser using echo, bypassing > the template altogether. A code example would help. > > > Initially I was sending to the template with a > $smarty->assign('variable', > > sprintf("%01.2f",$fee2)); > > > > I would go with Messju's suggestion, ie use: > $smarty->assign('variable',$fee2); > and in the template put: > {"%.2f"|sprintf:$variable} Firstly, the variable might have a price or it might have an explantion, depending on the web-service response. If I run the code that I have described as a pure php file, I get the same result as you - all is fine. but when I run it, and I have, when the output is using a Smarty template, I get the 160.00 result and the 'NULL' gettype. The actual code for this page is long and complex, but the relevant snippet looks like: =========================================================== $fee = [web-service response (in dollars and cents)] settype($fee, "float"); $fee_type = gettype($fee); $range = explode(",",strim($pr)); $rangecounter = count($range); $rangecounter = ($rangecounter>1?$rangecounter-1:0); $ccc_supp = floatval($rangecounter * 3); settype($ccc_supp, "float"); $fee2 = $fee + $ccc_supp; $smarty->assign('ccc_fee',($rt!='Granted'?'Not available through CCC: '.($rt!=''?$rt:'no reason given'):'The CCC QuickPrice for this request is: $'.sprintf("%01.2f",$fee2)."<br>$ccc_terms")); =========================================================== $pr is a pagerange and in the example is 9-27. extended range might be 1-3,9-27, which would then add $3 to the $fee variable. =========================================================== If I echo $fee, for my example, I get 160.44. If I echo $fee2, I get 160.00. But all I've done is add '0' to the $fee variable. Cheers George

« previous php.smarty.general (#13572) next »