Mark, et al,
I did some testing over the weekend and, as expected, can declare Smarty as
blameless, regarding my problem.
I've narrowed it down to the fact that my $fee variable is a simplexml
object. I now need to find a way of converting that to a float.
settype($fee, "float") doen't work.
Back to the PHP list.
Thanks for all suggestions.
Cheers
George
> -----Original Message-----
> From: Mark Rogers [mailto:mark@quarella.co.uk]
> Sent: 17 November 2006 6:12 pm
> To: George Pitcher
> Cc: Smarty-General
> Subject: Re: [SMARTY] problem displaying float
>
>
> 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
>
>
>