473,761 Members | 5,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiplication math not working ?

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_per cent is ".$number_perce nt."<br />";
$total=(($histo ry['order_total'])*$number_perce nt);
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
13 3335
Rik
One <da**********@g mail.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_per cent is ".$number_perce nt."<br />";
$total=(($histo ry['order_total'])*$number_perce nt);
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_per cent/100));
--
Rik Wasmus
Feb 7 '07 #2
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_per cent is ".$number_perce nt."<br />";
$total=(($histo ry['order_total'])*$number_perce nt);
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*******@attgl obal.net
=============== ===
Feb 7 '07 #3
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_per cent/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_r eplace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<b r />";
$total = ($removeit * (1+($number_per cent/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
One
On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attg lobal.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_r eplace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<b r />";
$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
Rik
One <da**********@g mail.comwrote:
On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
>$number_percen t 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_r eplace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<b r />";
$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_r eplace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<b r />";
$total = ($removeit * (1+($number_per cent/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($histo ry['order_total'])(or floatval($remov eit)) is....
--
Rik Wasmus
Feb 7 '07 #6
One wrote:
On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
>$number_percen t 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_r eplace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<b r />";
$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*******@attgl obal.net
=============== ===
Feb 7 '07 #7
One wrote:
On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
>$number_percen t 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_r eplace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<b r />";
$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*******@attgl obal.net
=============== ===
Feb 7 '07 #8
Rik
Jerry Stuckle <js*******@attg lobal.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
One
On Feb 7, 1:00 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
One wrote:
On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attg lobal.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_r eplace('$' ,'', $history['order_total']);
echo "strip the $ sign ".$removeit."<b r />";
$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_r eplace('$' ,'', $history['order_total']);
echo $removeit;
$floatandremove = floatval($remov eit);
echo "<br />floatandremo ve is ".$floatandremo ve;
exit;

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

This is getting crazy!

Feb 8 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

89
5141
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2 0.90000000000000013
17
3628
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels deep. In that case you need much more ram than a PC has to store functions calculated in loops so that you do not have to recalculate every time you cycle through the nest of loops. Using a HD for storage to extend ram is much too slow for many...
9
8001
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
0
1436
by: ccosse | last post by:
Hello, just to announce a new version of Multiplication Station available at http://www.asymptopia.org. Multiplication Station is an OpenSource math education game. It will teach your child basic maths, guaranteed. It is multi-user, has a countdown timer and tracks high scores. All parameters are configurable through the simple admin interface. Please visit to download a copy today. Certified GPL OpenSource. Asymptopia Software |...
110
8597
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm almost halfway through writing the over 200 functions needed to implement C99's version of math.h, and I would like to have some feedback and/or expert advice on my implementations. Sincerely, Gregory Pietsch
7
7588
by: VijaKhara | last post by:
Hi all, Is there any method which can implememt the matrix multiplication faster than using the formula as we often do by hand? I am writing the following code and my matrice: one is 3x40000 and the other one 40000x3. the program runs too slow: /*multiply two matrice: xyz_trans * xyz , the output is w 3x3*/
4
10894
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
3
3883
by: crazygrey | last post by:
Hello, I'm a newbie to C++ so excuse me if my question was trivial but it is important to me. I'm implementing a simple code to find the forward kinematics of a robot: #include "stdafx.h" #include<iostream> #include<iomanip> #include<fstream> #include"math.h"
1
9163
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void recMultiply(int i, int j, float a, int k, int l, float b, int x, int y, float c, int s); int i, j, k, s, matrixsize, blocksize, jj, kk, power, bsize; float sum, maxr, total=0.0, startmult, finishmult, multtime; float* A = NULL; float* B = NULL;
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.