This is a basic Math question, I think :-) Any help really appreciated. I can't get the following to balance i.e. the total of all line_items ($unconverted_amount) once converted is different to the converted invoice total. Is there a solution? Maybe I don't need Math::BigFloat. The amounts are all in pennies.
sub convert { use Math::BigFloat; my %currency_conv_hash = (IEP=>"EUR",GBP=>"GBP",USD=>"USD",EUR=>"EUR", FFR=>"EUR", NLG=>"EUR", ESP=>"EUR", DEM=>"EUR", ITL=>"EUR", ATS=>"EUR" +, BEF=>"EUR", PTE=>"EUR" ); my %rate_hash = (IEP=>".787564",GBP=>"1",USD=>"1",EUR=>"1", FFR=>"6.55 +957",NLG=>"2.20371", ESP=>"166.386", DEM=>"1.95583", ITL=>"1936.27", +ATS=>"13.7603", BEF=>"16.11", PTE=>"200.482" ); my($source_currency, $unconverted_amount, $unconverted_total_amount) = + @_; my $rate=$rate_hash{$source_currency}; my $line_amount = Math::BigFloat->new($unconverted_amount/$rat +e); my $total_amount = Math::BigFloat->new($unconverted_total_amount +/$rate); return ($currency_conv_hash{$source_currency}, (sprintf "%.0f",$line_ +amount->fround(0)), (sprintf "%.0f",$total_amount->fround(0)) ); } Now when I later total all the line amounts i.e. what is returned by s +printf "%.0f",$line_amount>fround(0)), the total is out by a penny or + so when compared to (sprintf "%.0f",$total_amount->fround(0))
Any assistance hugely appreciated.

In reply to Rounding errors problem by perlcgi

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.