Simple Maths Question I Hope

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PlateSpin
Forum Newbie
Posts: 9
Joined: Tue Sep 29, 2009 6:15 am

Simple Maths Question I Hope

Post by PlateSpin »

I have a HTML page that is sprinkled with PHP output giving me totals etc, an example such as:

<?PHP print $row[month_equiv_1]; ?>

The above would result in something like... £134.36

What I need to do is take the above number and multiply it by the output of another number generated in a similar fashion. Effectively what I'm trying to do is:

<?PHP print $row[month_equiv_1]; ?> * <?PHP print $row[week_equiv_1]; ?> = X

Then take X and subtract it from one last PHP print output.

I'm sure there's an easy way to do this but I'll be damned if I can see it. I could be wrong of course!!!!
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Simple Maths Question I Hope

Post by JakeJ »

How about this:

Code: Select all

<?php
$result = row[month_equiv_1] * row[week_equiv_1];
?>
Then you can use that anywhere on the page. Of course you don't even have to set that as a variable, you can just use following wherever you want to:

Code: Select all

<?php row[month_equiv_1] * row[week_equiv_1];
?>
To me, it's easier to use declare $result at the top of the page. It keeps the html below a little easier to read. Also, you don't have to go hunting for it later if you need to make changes. All your variables are set at the top.
PlateSpin
Forum Newbie
Posts: 9
Joined: Tue Sep 29, 2009 6:15 am

Re: Simple Maths Question I Hope

Post by PlateSpin »

I've tried out the above along with various other themes of the same ilk. I think it's the £ sign causing the trouble. It's being drawn in with the number answer, quite rightly so but it would appear to be the cause of my trouble. If I'm right in what I'm saying then this is not a simple maths type question at all.

Does that theory hold water or am I barking up the wrong tree? (woof woof!!!)
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Simple Maths Question I Hope

Post by JakeJ »

Post some code example so we can see what you're doing.
PlateSpin
Forum Newbie
Posts: 9
Joined: Tue Sep 29, 2009 6:15 am

Re: Simple Maths Question I Hope

Post by PlateSpin »

As I said before, I have a HTML page that is sprinkled with PHP output that contains nothing more than print statements. I can't easily access the parent script that fires off the page that I can see/manipulate. Besides, the parent script is almost 2,000 lines long and I wouldn't know where to start looking for something of relevance to my issue.

But it would appear that the £ sign is the thing giving me the trouble so with that in mind I'm using:

$new_string = ereg_replace("[^0-9.]", "", $string);

to strip out everything expect the numbers and the decimal point and it seems to work. I'm making the assumption that this is the best way forward but if you know better don't hold back.
lessthanphil
Forum Newbie
Posts: 1
Joined: Wed Jun 02, 2010 1:31 pm

Re: Simple Maths Question I Hope

Post by lessthanphil »

You're probably better off storing your currency values as a decimal without the string '£' saved with them....That way you can perform any mathematical calculations you need to without having issues such as the one you're already having.

You can append the '£' to them whenever you need to actually display their values.

I'm not sure where your data is coming from, so can't offer more advice than that.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Simple Maths Question I Hope

Post by Jonah Bron »

The regex replacement should be this:

Code: Select all

preg_replace("/[^0-9\.]/", "", $string);
ereg_replace() is depreciated.
Post Reply