You could avoid doing math all together and treat the problem as a string manipulation exercise:
#! perl -slw use strict; while( <DATA> ) { chomp; printf "Before '%s' becomes ", $_; s[ ( \d+ )+ (?: \. ( \d{0,2} ) ( \d* ) )? % ]{ local $^W; my $n = substr( $2 . '00', 0, 2 ); ($1||'') . $n . ( length($3) ? ".$3" : '' ); }xe; print "'$_'"; } __DATA__ 0.781063003540039% 2.25% 1.455% 4.9% 0.79% 75% 1.15% 0.45999999999999996% 0.9199999999999999% 0.22999999999999998% 1.3%
Gives:
c:\test>junk4 Before '0.781063003540039%' becomes '78.1063003540039' Before '2.25%' becomes '225' Before '1.455%' becomes '145.5' Before '4.9%' becomes '490' Before '0.79%' becomes '79' Before '75%' becomes '7500' Before '1.15%' becomes '115' Before '0.45999999999999996%' becomes '45.999999999999996' Before '0.9199999999999999%' becomes '91.99999999999999' Before '0.22999999999999998%' becomes '22.999999999999998' Before '1.3%' becomes '130'
In reply to Re: Maintain accuracy of floating point numbers in simple arithmetic operation
by BrowserUk
in thread Maintain accuracy of floating point numbers in simple arithmetic operation
by OfficeLinebacker
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |