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; }
In reply to Multiplication problem by John Vanbeek
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |