Hi Monks,

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.