Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Tag calculation or global?

Quote Reply
Tag calculation or global?
Hi,

I've got a field that I'm trying to calculate tax on and then display. This is the template tag I'm using...

<% Price * 1.125 %>

but what I want to do is get it to round the result to two decimal places using these rules:

1. If the 3rd number after decimal point is 4 or below, it rounds the result down.
2. If the 3rd number after decimal point is 5 or above, it rounds the result up.

Can anyone help me out with how? It's sounding like a global job as I'm not sure if it's something that can be done in the templates.

Thanks

Regan.
Quote Reply
Re: [ryel01] Tag calculation or global? In reply to
I think your tag should be:

<% Price * '1.125' %>

...but anyway, you should use global I think.

<%GlobalName($Price)%>

Code:
sub {
return sprintf ("%.2f", ($_[0] * 1.125));
}

Last edited by:

Paul: Jun 13, 2002, 2:06 AM
Quote Reply
Re: [Paul] Tag calculation or global? In reply to
hi paul

thanks for that!

it works well accept it doesn't round up if the 3rd number after the decimal is a 5 - it goes down.

i'll have to figure out some code to do the maths on that one, unless you know of a function?

thanks again,
regan.
Quote Reply
Re: [ryel01] Tag calculation or global? In reply to
sprintf should already do that....when I tested I did:

Code:
#!/perl/bin/perl

main(1);

sub main {
#---------------------------------------------------------
print "Content-type: text/html\n\n";
print sprintf ("%.2f", ($_[0] * 1.125));
}

When I used 1.125 the answer was 1.3 and when I used 1.124 the answer was 1.2
Quote Reply
Re: [Paul] Tag calculation or global? In reply to
hi paul,

I'm using a value of 9.08 in my test, which on a calculator comes to 10.215 - because that ends with a 5 I'd like to round it up.

the code is displaying the result as 10.21 though - give it a go with that number on your setup and let me know what you get.

cheers,
regan.

Last edited by:

ryel01: Jun 13, 2002, 2:46 AM
Quote Reply
Re: [ryel01] Tag calculation or global? In reply to
I get 10.22

Code:
#!/perl/bin/perl

main(9.08);

sub main {
print "Content-type: text/html\n\n";
print sprintf ("%.2f", ($_[0] * 1.125));
}

Last edited by:

Paul: Jun 13, 2002, 2:57 AM
Quote Reply
Re: [Paul] Tag calculation or global? In reply to
hi paul,

strange. do you know if there's a setting or anything anywhere that alters the way the rounding is done?

regan.
Quote Reply
Re: [Paul] Tag calculation or global? In reply to
I get 10.21 ....

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Tag calculation or global? In reply to
Hmm thats a bit weird...I promise you I'm not lying Smile...I get 10.22 every time.

What version of perl do you both have?

I'm using 5.6.1 from Activestate.
Quote Reply
Re: [Paul] Tag calculation or global? In reply to
Quote:
This is perl, v5.6.1 built for i386-linux

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [ryel01] Tag calculation or global? In reply to
I guess if thats not working for you, you may want to install Math::Round if you can then you can do:

sub {
require Math::Round;
return round( $_[0] * 1.125);
}
Quote Reply
Re: [Paul] Tag calculation or global? In reply to
version 5.005_03 built for i386-linux

*cough*

that's on my development server.

i'll try installing Math::Round and see what happens.

thanks again,

regan.
Quote Reply
Re: [Paul] Tag calculation or global? In reply to
thanks for the suggestion about installing Math::Round paul!

have done that and it's working perfectly now - not sure what was happening with my version of perl rounding differently to yours... ahh well.

this was the final solution...


1. install module Math::Round

2. create global called "priceincludingtax"
Code:
sub {

use Math::Round qw(:all);
return nearest(.01, $_[0] * 1.125);

}

2. use this tag in your template...

Code:
<%priceincludingtax ($Price)%>

where $Price is another database field of your link.

cheers,
regan.


ps. out of interest it returned 10.21 with perl v5.6.0 built for i386-linux too - weird.

Last edited by:

ryel01: Jun 15, 2002, 1:11 AM
Quote Reply
Re: [ryel01] Tag calculation or global? In reply to
Hi,

I was dealing with the same "problem" and wanted to use a tag not a global. I tried to use the /N function wich is implemented now but it seems there is only one operation per tag available so in your case I would have ended up with the working but not so nice:

Code:

<%Price /2 0.88888888888888%>


As I was looking for a smoother solution and found this post just wanted to add my experience for information.

Regards

n | i | k | o