PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.smarty.general > problem displaying float
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
problem displaying float

Réponse
 
LinkBack Outils de la discussion
Vieux 17/11/2006, 11h52   #1
George Pitcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut problem displaying float

Hi,

I'm having a problem - only on Smarty pages.

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);

gives me

160.44
160.44
float

on a non-Smarty page
and

160.44
160
NULL

when echo'd on a page using a Smarty template.

I've tried using sprintf() with no success.

Does anyone have any idea why this is happening?

George in Oxford
  Réponse avec citation
Vieux 17/11/2006, 12h49   #2
Mark Rogers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] problem displaying float

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);
>


I'd be more interested to know what gettype shows before the settype,
and also for gettype($fee2).

> [...]
> and
>
> 160.44
> 160
> NULL
>
> when echo'd on a page using a Smarty template.
>


What does your Smarty template look like? Where are you doing the
"maths" (the +0 bit) - within the template?

I'm really not clear what you're assigning to the template, or how
you're showing it.

--
Mark Rogers
More Solutions Ltd :: 0845 45 89 555
  Réponse avec citation
Vieux 17/11/2006, 12h58   #3
messju mohr
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] problem displaying float

On Fri, Nov 17, 2006 at 11:52:23AM -0000, George Pitcher wrote:
[...]
> I've tried using sprintf() with no success.


{"%.2f"|sprintf:$fee}

greetings
messju

> Does anyone have any idea why this is happening?
>
> George in Oxford

  Réponse avec citation
Vieux 17/11/2006, 13h11   #4
George Pitcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [SMARTY] problem displaying float

> > 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);
> >

>
> I'd be more interested to know what gettype shows before the settype,
> and also for gettype($fee2).
>
> > [...]
> > and
> >
> > 160.44
> > 160
> > NULL
> >
> > when echo'd on a page using a Smarty template.
> >

>
> What does your Smarty template look like? Where are you doing the
> "maths" (the +0 bit) - within the template?
>
> I'm really not clear what you're assigning to the template, or how
> you're showing it.
>

Mark,

gettype($fee) before settype() gave me 'object'.

I'm doing all this in the php and sending it to the template using echo.
Initially I was sending to the template with a $smarty->assign('variable',
sprintf("%01.2f",$fee2));

Hope trhat's a bit clearer.

George
  Réponse avec citation
Vieux 17/11/2006, 13h43   #5
Mark Rogers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] problem displaying float

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 .

> 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}

--
Mark Rogers
More Solutions Ltd :: 0845 45 89 555
  Réponse avec citation
Vieux 17/11/2006, 14h10   #6
George Pitcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [SMARTY] problem displaying float

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 .
>
> > 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
  Réponse avec citation
Vieux 17/11/2006, 15h27   #7
messju mohr
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] problem displaying float

On Fri, Nov 17, 2006 at 02:10:12PM -0000, 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"));
> ================================================== =========
> $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.


this is your third post on the topic and you still refused to show us a
single line of template code.

now you show us a case where you trigger the error without any smarty
interference (I think we can ignore the $smarty->assign() and your
error occurs with or without it).

so what has this to do with smarty anyway?

greetings
messju

> Cheers
>
> George

  Réponse avec citation
Vieux 17/11/2006, 15h44   #8
George Pitcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [SMARTY] problem displaying float

> On Fri, Nov 17, 2006 at 02:10:12PM -0000, 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"));
> > ================================================== =========
> > $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.

>
> this is your third post on the topic and you still refused to show us a
> single line of template code.
>
> now you show us a case where you trigger the error without any smarty
> interference (I think we can ignore the $smarty->assign() and your
> error occurs with or without it).
>
> so what has this to do with smarty anyway?
>

messju

I do all my calculations in PHP and let Smarty handle the display - I
thought that's what templates were all about.

The problem I have is that my display via $smarty-assign() was giving me an
incorrect display.

I then, as usual with such an error, thought there was something wrong with
the PHP code, so started echoing the output at the various steps. I still
thought the problem was in PHP, but when I decided to put the process into a
php file that did not include and $smarty interaction, the display was fine,
which is why I'm asking the question on this list.

If no-one has a solution, I'll need to think of a different way to display
the price to my users.

Cheers

George
  Réponse avec citation
Vieux 17/11/2006, 16h05   #9
Guido Moonen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [SMARTY] problem displaying float

Hi,

So what are you using to print the variable in the template?

e.g.
{$variable}

Or {php}echo $variable{/php}

Or what?

And do you use any modifier for the variable, or do you just use the
variable as assigned to?

Guido

-----Oorspronkelijk bericht-----
Van: George Pitcher [mailto:george.pitcher@ingenta.com]
Verzonden: vrijdag 17 november 2006 16:45
Aan: Smarty-General
Onderwerp: RE: [SMARTY] problem displaying float

> On Fri, Nov 17, 2006 at 02:10:12PM -0000, 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"));
> > ================================================== =========
> > $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.

>
> this is your third post on the topic and you still refused to show us a
> single line of template code.
>
> now you show us a case where you trigger the error without any smarty
> interference (I think we can ignore the $smarty->assign() and your
> error occurs with or without it).
>
> so what has this to do with smarty anyway?
>

messju

I do all my calculations in PHP and let Smarty handle the display - I
thought that's what templates were all about.

The problem I have is that my display via $smarty-assign() was giving me an
incorrect display.

I then, as usual with such an error, thought there was something wrong with
the PHP code, so started echoing the output at the various steps. I still
thought the problem was in PHP, but when I decided to put the process into a
php file that did not include and $smarty interaction, the display was fine,
which is why I'm asking the question on this list.

If no-one has a solution, I'll need to think of a different way to display
the price to my users.

Cheers

George

--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
  Réponse avec citation
Vieux 17/11/2006, 16h05   #10
Peggy Schatz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] problem displaying float

Hi George,

maybe you did not assign $fee properly/at all...

There should be a line of code in your php-script like:

$smarty->assign('fee',$fee);

Within your template you then state {$fee} or as messju recommended
{"%.2f"|sprintf:$fee}
The latter modifies a string to get the proper number of floating values
(or however these are called)....

Hope that s...

Peggy
  Réponse avec citation
Vieux 17/11/2006, 16h12   #11
George Pitcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [SMARTY] problem displaying float

Guido and Peggy,

> So what are you using to print the variable in the template?
>
> e.g.
> {$variable}
>
> Or {php}echo $variable{/php}



$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','The CCC QuickPrice for this request is:
$'.sprintf("%01.2f",$fee2)."<br>$ccc_terms");

>
> Or what?
>
> And do you use any modifier for the variable, or do you just use the
> variable as assigned to?


> maybe you did not assign $fee properly/at all...
>
> There should be a line of code in your php-script like:


> $smarty->assign('fee',$fee);


> Within your template you then state {$fee} or as messju recommended
> {"%.2f"|sprintf:$fee}
> The latter modifies a string to get the proper number of floating values
> (or however these are called)....


All is handled in PHP before assignation. I'm only asking $smarty to display
my variable.

I've been using Smarty for 3 years now - across over 20 websites and this is
the first real hassle I've encountered.

Anyway, I'm off for the weekend now.

Cheers

George
  Réponse avec citation
Vieux 17/11/2006, 16h24   #12
Guido Moonen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [SMARTY] problem displaying float

Hi George,

My test script using your example does not show anything wrong:

My PHP:
> <?php
>
> include_once "Smarty/Smarty.class.php";
>
> $fee = 1.22;
> settype($fee, "float");
> $fee_type = gettype($fee);
> $range = explode(",","1-3");
> $rangecounter = count($range);
> $rangecounter = ($rangecounter>1?$rangecounter-1:0);
> $ccc_supp = floatval($rangecounter * 3);
> settype($ccc_supp, "float");
> $fee2 = $fee + $ccc_supp;
>
> echo 'The CCC QuickPrice for this request is: $' .
> sprintf("%01.2f",$fee2)."<br>$ccc_terms";
>
> $rt = "Granted";
>
> // create object
> $smarty = new Smarty;
> $smarty->assign('fcc',($rt!='Granted'?'Not available through CCC:' .
> ($rt!=''?$rt:'noreason given'):'The CCC QuickPrice for this request is: $'
> . sprintf("%01.2f",$fee2)."<br>$ccc_terms"));
>
> $smarty->template_dir = "D:\\web\\WAMP\\www";
> $smarty->compile_dir = "D:\\web\\WAMP\\www\\templates_c";
> // display it
> $smarty->display('test.tpl');
>
> ?>


My Template "test.tpl"
> {$fcc}


The result when I use this code through the browser:
> The CCC QuickPrice for this request is: $1.22
> The CCC QuickPrice for this request is: $1.22


Also for smarty you will always need a template. Thus your code should
contain a $smarty->display(filename) which tell you which template you are
using. Smarty is not a pretty printing tool, but a template engine. So we
need variables (Assigned by $smarty->assign) and a template (Gives by
$smarty->display) to create a result.

Cheers,
Guido

-----Oorspronkelijk bericht-----
Van: George Pitcher [mailto:george.pitcher@ingenta.com]
Verzonden: vrijdag 17 november 2006 16:45
Aan: Smarty-General
Onderwerp: RE: [SMARTY] problem displaying float

> On Fri, Nov 17, 2006 at 02:10:12PM -0000, 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"));
> > ================================================== =========
> > $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.

>
> this is your third post on the topic and you still refused to show us a
> single line of template code.
>
> now you show us a case where you trigger the error without any smarty
> interference (I think we can ignore the $smarty->assign() and your
> error occurs with or without it).
>
> so what has this to do with smarty anyway?
>

messju

I do all my calculations in PHP and let Smarty handle the display - I
thought that's what templates were all about.

The problem I have is that my display via $smarty-assign() was giving me an
incorrect display.

I then, as usual with such an error, thought there was something wrong with
the PHP code, so started echoing the output at the various steps. I still
thought the problem was in PHP, but when I decided to put the process into a
php file that did not include and $smarty interaction, the display was fine,
which is why I'm asking the question on this list.

If no-one has a solution, I'll need to think of a different way to display
the price to my users.

Cheers

George

--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
  Réponse avec citation
Vieux 17/11/2006, 16h49   #13
messju mohr
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] problem displaying float

smarty displays what you assign. nothing more nothing less.
again: the fact that you use smarty does not make this a smarty
issue.

maybe you have some locale settings that disturb your
float-conversions?

like this braindead behaviour of php:

setlocale(LC_ALL, "de_DE.UTF-8");
$foo = 10001/100;
echo $foo, "\n";
echo floatval("$foo"), "\n";

gives on my system:
100,01
100

On Fri, Nov 17, 2006 at 04:12:19PM -0000, George Pitcher wrote:
> Guido and Peggy,
>
> > So what are you using to print the variable in the template?
> >
> > e.g.
> > {$variable}
> >
> > Or {php}echo $variable{/php}

>
>
> $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','The CCC QuickPrice for this request is:
> $'.sprintf("%01.2f",$fee2)."<br>$ccc_terms");
>
> >
> > Or what?
> >
> > And do you use any modifier for the variable, or do you just use the
> > variable as assigned to?

>
> > maybe you did not assign $fee properly/at all...
> >
> > There should be a line of code in your php-script like:

>
> > $smarty->assign('fee',$fee);

>
> > Within your template you then state {$fee} or as messju recommended
> > {"%.2f"|sprintf:$fee}
> > The latter modifies a string to get the proper number of floating values
> > (or however these are called)....

>
> All is handled in PHP before assignation. I'm only asking $smarty to display
> my variable.
>
> I've been using Smarty for 3 years now - across over 20 websites and this is
> the first real hassle I've encountered.
>
> Anyway, I'm off for the weekend now.
>
> Cheers
>
> George

  Réponse avec citation
Vieux 17/11/2006, 17h14   #14
Max Schwanekamp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] problem displaying float

George Pitcher wrote:
> I do all my calculations in PHP and let Smarty handle the display - I
> thought that's what templates were all about.


Exactly. Assign the value to the template object as a string, not as a
float, and see what you get. Shouldn't matter, but at least then you'll
get the value in a more-or-less literal form. There generally oughtn't
be any need to using sprintf() in the template code. Also, do you have
error_reporting set to at least E_ALL?

--
Max Schwanekamp
NeptuneWebworks.com
541-517-9064
  Réponse avec citation
Vieux 17/11/2006, 17h56   #15
Mark Rogers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] problem displaying float

George Pitcher wrote:
> Guido and Peggy,
>
>> So what are you using to print the variable in the template?

> $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','The CCC QuickPrice for this request is:
> $'.sprintf("%01.2f",$fee2)."<br>$ccc_terms");
>


None of the above displays anything; you assign a value to the Smarty,
but don't show the call to $smarty->display() or (and this is what both
I and others have asked for) what you put in the template. For the sake
of argument I'll assume your template just says
{$ccc_fee}

I think it's clear that the value being assigned is wrong, so this isn't
a Smarty issue, but a PHP one. This list gets more than its fair share
of off-topic PHP queries hence some reluctance to assist in non-Smarty
problems. However, I'll make some suggestions then bow out of the topic
(I'll assist off-list if you're really stuck).

Why are you using lots of settype() calls? Is that the result of some
hacks to try and resolve this problem? It's very rare that settype
should really be needed anywhere.

What does the following code give you?

$fee = [web-service response (in dollars and cents)]
var_dump($fee);
$range = explode(",",strim($pr));
$rangecounter = count($range);
$rangecounter = ($rangecounter>1?$rangecounter-1:0);
$ccc_supp = $rangecounter * 3;
var_dump($range, $rangecounter, $ccc_supp);
$fee2 = $fee + $ccc_supp;
var_dump($fee2, sprintf("%01.2f",$fee2));

Note that I use var_dump as that tells you both the value and the type in one.


> All is handled in PHP before assignation. I'm only asking $smarty to display
> my variable.
>


The implication, which there's been nothing to suggest to the contrary
here, is therefore that the variable is wrong before it gets to Smarty.
Unless, that is, you're doing something in Smarty other than just
displaying what its sent, such as formatting code. That's why seeing the
relevant part of the template would .

> I've been using Smarty for 3 years now - across over 20 websites and this is
> the first real hassle I've encountered.
>


I'm 90% sure this is nothing to do with Smarty, but I'm quite open to
being proved wrong on that.

--
Mark Rogers
More Solutions Ltd :: 0845 45 89 555
  Réponse avec citation
Vieux 17/11/2006, 18h12   #16
Mark Rogers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 20/11/2006, 07h52   #17
George Pitcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [SMARTY] problem displaying float

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
>
>
>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h04.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,32409 seconds with 25 queries