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
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 20h01.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15574 seconds with 16 queries