By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,729 Members | 1,433 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,729 IT Pros & Developers. It's quick & easy.

multiplication math not working ?

P: n/a
One
hi group -

I had this in a function - so I've just moved this out of the function
for readabilty.

Can someone please tell me WHY does this not return the result of :
n1 times n2

As I said - I took it out of the function just for readability :
In my php file :

$number_percent="25";
echo "history['order_total'] is ".$history['order_total']."<br />";
echo "number_percent is ".$number_percent."<br />";
$total=(($history['order_total'])*$number_percent);
echo "total is ".$total."<br />";

And here is the output in the browser :
history['order_total'] is $241.47
number_percent is 25
total is 0

WHY ? Any advice or pointers would be much appreciated!

Feb 7 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Rik
One <da**********@gmail.comwrote:
hi group -

I had this in a function - so I've just moved this out of the function
for readabilty.

Can someone please tell me WHY does this not return the result of :
n1 times n2

As I said - I took it out of the function just for readability :
In my php file :

$number_percent="25";
echo "history['order_total'] is ".$history['order_total']."<br />";
echo "number_percent is ".$number_percent."<br />";
$total=(($history['order_total'])*$number_percent);
echo "total is ".$total."<br />";

And here is the output in the browser :
history['order_total'] is $241.47
number_percent is 25
total is 0

WHY ? Any advice or pointers would be much appreciated!
Because '$241.47' is not a number, it's a string representation of a
currency. PHP doesn't know anything about that, 'currency' is not a type..
When performing math on it, PHP will try to make a number out of it, and
encounters '$' as the first character. This is not something it
recognizes, and it will default to 0. And 0 * 25 = 0.

You might want to remove the currency symbol from the string, and just add
it back when you have to echo/print the total.

Also, I'd think if $number_percent = 25, you might want $total =
$history['order_total'] * (1+($number_percent/100));
--
Rik Wasmus
Feb 7 '07 #2

P: n/a
One wrote:
hi group -

I had this in a function - so I've just moved this out of the function
for readabilty.

Can someone please tell me WHY does this not return the result of :
n1 times n2

As I said - I took it out of the function just for readability :
In my php file :

$number_percent="25";
echo "history['order_total'] is ".$history['order_total']."<br />";
echo "number_percent is ".$number_percent."<br />";
$total=(($history['order_total'])*$number_percent);
echo "total is ".$total."<br />";

And here is the output in the browser :
history['order_total'] is $241.47
number_percent is 25
total is 0

WHY ? Any advice or pointers would be much appreciated!
$number_percent is a string containing 25, not the numeric value 25.
When you use a string in a calculation it's numeric value is zero.

Try
$number_percent=25;

instead (and ensure that $history['order_total'] is also numeric).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 7 '07 #3

P: n/a
One
Because '$241.47' is not a number, it's a string representation of a
currency. PHP doesn't know anything about that, 'currency' is not a type.
When performing math on it, PHP will try to make a number out of it, and
encounters '$' as the first character. This is not something it
recognizes, and it will default to 0. And 0 * 25 = 0.

You might want to remove the currency symbol from the string, and just add
it back when you have to echo/print the total.

Also, I'd think if $number_percent = 25, you might want $total =
$history['order_total'] * (1+($number_percent/100));
Hi Rik - thank you the reply.

I used string replace to remove the dollar sign, then I simply
multiplied the two numbers, and it is still calculating as zero.
Can I cast this variable to an int ??

Input :
$number_percent="25";
echo "history['order_total'] is ".$history['order_total']."<br />";
$removeit=str_replace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<br />";
$total = ($removeit * (1+($number_percent/100)));
echo "<br />total is ".$total."<br />";

Output :
history['order_total'] is $241.47
strip the $ sign 241.47
total is 0
:-(


Feb 7 '07 #4

P: n/a
One
On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
$number_percent is a string containing 25, not the numeric value 25.
When you use a string in a calculation it's numeric value is zero.

Try
$number_percent=25;

instead (and ensure that $history['order_total'] is also numeric).
Hi Jerry - thanks much for trying.
Same result :
Input :
$number_percent=25;
echo "history['order_total'] is ".$history['order_total']."<br />";
$removeit=str_replace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<br />";
$total = ($removeit * $number_percent);
echo "<br />total is ".$total."<br />";

Output :
history['order_total'] is $241.47
strip the $ sign 241.47
total is 0

This is bonkers - I'm *sure* I have done this type of thing in the
past without issue. :-(
Feb 7 '07 #5

P: n/a
Rik
One <da**********@gmail.comwrote:
On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>$number_percent is a string containing 25, not the numeric value 25.
When you use a string in a calculation it's numeric value is zero.

Try
$number_percent=25;

instead (and ensure that $history['order_total'] is also numeric).

Hi Jerry - thanks much for trying.
Same result :
Input :
$number_percent=25;
echo "history['order_total'] is ".$history['order_total']."<br />";
$removeit=str_replace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<br />";
$total = ($removeit * $number_percent);
echo "<br />total is ".$total."<br />";

Output :
history['order_total'] is $241.47
strip the $ sign 241.47
total is 0

This is bonkers - I'm *sure* I have done this type of thing in the
past without issue. :-(
This works here:
<?php
$history['order_total'] = '$241.47';
$number_percent="25";//should be without the quotes indeed, but just to
show PHP can cast this correctly
echo "history['order_total'] is ".$history['order_total']."<br />";
$removeit=str_replace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<br />";
$total = ($removeit * (1+($number_percent/100)));
echo "<br />total is ".$total."<br />";
?>

So I'd say there's something wrong with the exact contents of
$history['order_total'] in your script.

What if you do this (a bit overkill):

$removeit = preg_replace('/[^0-9.]/','',$history['order_total']);

If this doesn't work, I'm very curious what the output of
floatval($history['order_total'])(or floatval($removeit)) is....
--
Rik Wasmus
Feb 7 '07 #6

P: n/a
One wrote:
On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>$number_percent is a string containing 25, not the numeric value 25.
When you use a string in a calculation it's numeric value is zero.

Try
$number_percent=25;

instead (and ensure that $history['order_total'] is also numeric).

Hi Jerry - thanks much for trying.
Same result :
Input :
$number_percent=25;
echo "history['order_total'] is ".$history['order_total']."<br />";
$removeit=str_replace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<br />";
$total = ($removeit * $number_percent);
echo "<br />total is ".$total."<br />";

Output :
history['order_total'] is $241.47
strip the $ sign 241.47
total is 0

This is bonkers - I'm *sure* I have done this type of thing in the
past without issue. :-(

Rik caught one and I caught the other (I missed the dollar sign).

Both are string in your setup. Use floatval() to convert your
$history['order_total'] to a floating point number.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 7 '07 #7

P: n/a
One wrote:
On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>$number_percent is a string containing 25, not the numeric value 25.
When you use a string in a calculation it's numeric value is zero.

Try
$number_percent=25;

instead (and ensure that $history['order_total'] is also numeric).

Hi Jerry - thanks much for trying.
Same result :
Input :
$number_percent=25;
echo "history['order_total'] is ".$history['order_total']."<br />";
$removeit=str_replace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<br />";
$total = ($removeit * $number_percent);
echo "<br />total is ".$total."<br />";

Output :
history['order_total'] is $241.47
strip the $ sign 241.47
total is 0

This is bonkers - I'm *sure* I have done this type of thing in the
past without issue. :-(

Better yet - store the data as a floating point value without the dollar
sign. Then insert the dollar sign when you're ready to print.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 7 '07 #8

P: n/a
Rik
Jerry Stuckle <js*******@attglobal.netwrote:
Better yet - store the data as a floating point value without the dollar
sign. Then insert the dollar sign when you're ready to print.
Indeed, check money_format().
--
Rik Wasmus
Feb 7 '07 #9

P: n/a
One
On Feb 7, 1:00 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
One wrote:
On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
$number_percent is a string containing 25, not the numeric value 25.
When you use a string in a calculation it's numeric value is zero.
Try
$number_percent=25;
instead (and ensure that $history['order_total'] is also numeric).
Hi Jerry - thanks much for trying.
Same result :
Input :
$number_percent=25;
echo "history['order_total'] is ".$history['order_total']."<br />";
$removeit=str_replace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<br />";
$total = ($removeit * $number_percent);
echo "<br />total is ".$total."<br />";
Output :
history['order_total'] is $241.47
strip the $ sign 241.47
total is 0
This is bonkers - I'm *sure* I have done this type of thing in the
past without issue. :-(

Rik caught one and I caught the other (I missed the dollar sign).

Both are string in your setup. Use floatval() to convert your
$history['order_total'] to a floating point number.
Hi guys - thanks SO much for your replies - it is still not working -
I'm using the floatval properly but it is returning a zero :

Input :
echo "history['order_total'] is ".$history['order_total']."<br />";
$removeit=str_replace('$' ,'', $history['order_total']);
echo $removeit;
$floatandremove = floatval($removeit);
echo "<br />floatandremove is ".$floatandremove;
exit;

Output :
history['order_total'] is $241.47
241.47
floatandremove is 0

This is getting crazy!

Feb 8 '07 #10

P: n/a
Rik
One <da**********@gmail.comwrote:
Input :
echo "history['order_total'] is ".$history['order_total']."<br />";
$removeit=str_replace('$' ,'', $history['order_total']);
echo $removeit;
$floatandremove = floatval($removeit);
echo "<br />floatandremove is ".$floatandremove;
exit;

Output :
history['order_total'] is $241.47
241.47
floatandremove is 0
There must be something else going on with your currency string.
Please use this on that string, and report it's results back to us:

echo preg_replace('/(.)/se','"[".ord("$1")."]"',$history['order_total']);
--
Rik Wasmus
Feb 8 '07 #11

P: n/a
One
On Feb 8, 2:01 pm, Rik <luiheidsgoe...@hotmail.comwrote:
One <david.hun...@gmail.comwrote:
Input :
echo "history['order_total'] is ".$history['order_total']."<br />";
$removeit=str_replace('$' ,'', $history['order_total']);
echo $removeit;
$floatandremove = floatval($removeit);
echo "<br />floatandremove is ".$floatandremove;
exit;
Output :
history['order_total'] is $241.47
241.47
floatandremove is 0

There must be something else going on with your currency string.
Please use this on that string, and report it's results back to us:

echo preg_replace('/(.)/se','"[".ord("$1")."]"',$history['order_total']);
--
Rik Wasmus
Thanks again Rik - here you go :

Input :
echo "history['order_total'] is ".$history['order_total']."<br />";
echo preg_replace('/(.)/se','"[".ord("$1")."]"',
$history['order_total']);
exit;

Output :
history['order_total'] is $241.47
[60][98][62][36][50][52][49][46][52][55][60][47][98][62]

Feb 8 '07 #12

P: n/a
One wrote:
history['order_total'] is $241.47
[60][98][62][36][50][52][49][46][52][55][60][47][98][62]
$history['order_total'] contains '<b>$241.47</b>'. This is not a number.

$history['order_total'] = (float)preg_replace('/[^0-9\.]/', '',
strip_tags($history['order_total']));

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 9 '07 #13

P: n/a
One
On Feb 9, 5:51 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
One wrote:
history['order_total'] is $241.47
[60][98][62][36][50][52][49][46][52][55][60][47][98][62]

$history['order_total'] contains '<b>$241.47</b>'. This is not a number.

$history['order_total'] = (float)preg_replace('/[^0-9\.]/', '',
strip_tags($history['order_total']));

Mr. Toby!
You da man - thanks very much!

Thank you also to Rik and Jerry for taking the time to resolve this
issue.
Feb 12 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.