Re: [SMARTY] problem displaying float
[email protected] (Mark Rogers) Fri, 17 Nov 2006 18:12:07 +0000
| Newsgroups | php.smarty.general |
|---|---|
| Organization | Quarella Ltd |
| Message-ID | <[email protected]> |
George Pitcher wrote:
> 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"));
> ===========================================================
>
I just tried the following code, which I suggest you also try:
===========================================
<?php
$fee = '160.44';
$pr = '9-27';
$ccc_terms = '';
$rt = 'Granted';
settype($fee, "float");
$fee_type = gettype($fee);
$range = explode(",",trim($pr));
$rangecounter = count($range);
$rangecounter = ($rangecounter>1?$rangecounter-1:0);
$ccc_supp = floatval($rangecounter * 3);
settype($ccc_supp, "float");
$fee2 = $fee + $ccc_supp;
$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";
var_dump($fee, $range, $rangecounter, $ccc_supp, $fee2, $ccc_fee);
?>
===========================================
It is as close I could get to your exact code, notice I substitute
"trim" for "strim", and calculated and 'var_dump'ed $ccc_fee rather than
using Smarty. The results I get are:
===========================================
float(160.44)
array(1) {
[0]=>
string(4) "9-27"
}
int(0)
float(0)
float(160.44)
string(51) "The CCC QuickPrice for this request is: $160.44<br>"
===========================================
All of those look fine to me. If you get the same results, and yet
$smarty->assign('ccc_fee',$ccc_fee);
.. in your code and
{$ccc_fee}
.. in your template shows something different, then I would be very
surprised.
However, suppose $fee is actually '160. 44' (note the space after the
.), or as Messju suggested '160,44' (comma instead of .) then the
conversion to float will give 160 not 160.44 and the results you are
getting. So corruption of the value of $fee would be where I'd go looking.
You should also be careful that a price of '1,234.44' doesn't get
converted to $1 by your method, for the same reasons.
--
Mark Rogers
More Solutions Ltd :: 0845 45 89 555