John Vanbeek has asked for the wisdom of the Perl Monks concerning the following question:
I'm using a webshop program called hishop. Recently a discovered a bug. The problem is that when an article has a price of 17.95 and the quantity is 3 the program makes a multiplication error. The total should be 53.85, but the program gives 53.84. Using a price of 17.96, 17.94 or 10.95 gives the correct result.
The program uses a subprogram called round(), this is where it goes wrong. My knowledge of perl is not sufficient to find the error.
I made an example program. Please help.
Best regards, John Vanbeek#!/usr/bin/perl $price=17.95; $qtyx=3; $cost=round($price * $qtyx); $cost=commas($cost);$price=round($price);$price=commas($price); print "Content-type: text/html\n\n"; print "total costs are $cost"; sub round { my($fig)=@_; $fig=($fig * 100); if($fig=~/\./){ $fig=~m!^([0-9]*)\.([0-9])!;$fig=$1;$ext=$2; if($ext>4){$fig++;} } # end if($fig=~/\./); $fig=int($fig); if($fig=~/^[0-9][0-9]$/){$fig="0$fig";} $fig=~s!^([0-9]+)([0-9][0-9])$!\1\.\2!; if($fig eq '0'){$fig='0.00';} return($fig); } ###################################################################### +####################### sub commas { my($fig)=@_; $fig=~s!\,!!g; my ($figx,$ext)=split(/\./,$fig); $ext=($ext)?"\.$ext":''; $figx=~s!^([0-9]+)([0-9]{3})$!\1\,\2!; while($figx=~/^([0-9]+)([0-9]{3})\,(.*?)$/){$figx=~s!^([0-9]+)([0-9]{3 +})\,(.*?)$!\1\,\2\,\3!;} $figx=($figx.$ext); return $figx; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multiplication problem
by mwah (Hermit) on Dec 10, 2007 at 15:23 UTC | |
| |
|
Re: Multiplication problem
by dragonchild (Archbishop) on Dec 10, 2007 at 14:50 UTC | |
by John Vanbeek (Initiate) on Dec 10, 2007 at 15:20 UTC | |
by dragonchild (Archbishop) on Dec 10, 2007 at 15:44 UTC | |
by John Vanbeek (Initiate) on Dec 10, 2007 at 16:28 UTC | |
by dragonchild (Archbishop) on Dec 11, 2007 at 20:56 UTC | |
|
Re: Multiplication problem
by goibhniu (Hermit) on Dec 10, 2007 at 16:28 UTC | |
|
Re: Multiplication problem
by oha (Friar) on Dec 10, 2007 at 16:51 UTC | |
|
Re: Multiplication problem
by rgiskard (Hermit) on Dec 10, 2007 at 18:01 UTC | |
|
Re: Multiplication problem
by pc88mxer (Vicar) on Dec 10, 2007 at 18:45 UTC | |
|
Re: Multiplication problem
by Roy Johnson (Monsignor) on Dec 10, 2007 at 17:00 UTC | |
|
Re: Multiplication problem
by swampyankee (Parson) on Dec 11, 2007 at 00:07 UTC |