multiply help

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
andrew99
Forum Newbie
Posts: 7
Joined: Mon Dec 08, 2003 9:01 am

multiply help

Post by andrew99 »

I have a var with a value of 35.00.
I want it to increase by 15% so i wrote

Code: Select all

<?php

$new_var = $old_var*1,15; //error
// or
$new_var = $old_var*1.15; // error
//or
$new_var = $old_var+($old_var*15/100); // new value is irrelevant 34.5

?>
where i am wrong? the original value is from a mysql db stored as (decimal 8,2).
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

what error are you getting because

Code: Select all

$new_var = $old_var*1.15;
works for me

Mark
andrew99
Forum Newbie
Posts: 7
Joined: Mon Dec 08, 2003 9:01 am

Post by andrew99 »

with

$new_var = $old_var*1.15;

and $old_var=35.00

i get a result of 34.5 instead of 40.25

ERROR!!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

the result i get is 40.25

Weird!

Mark
krash_control
Forum Newbie
Posts: 14
Joined: Mon Jan 12, 2004 10:02 am
Location: United Kingdom
Contact:

Post by krash_control »

I get the same as Bech100

Just wondering if this could have anything to do with the type of old_var? I am still a bit new to PHP but is there such a thing as type casting in PHP. That is, converting $old_var to integer or double to make sure it calculates correctly
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

you can type cast, but usually it isn't neccessary

check out [php_man]settype()[/php_man]

Mark
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Sounds like time to check if $old_var really does have the value you expect:

Code: Select all

<?php
echo $old_var . '<br />';
$new_var = $old_var*1.15; 
?>
Post Reply