In cb, you indicated that the general problem is to do a Banker's Round, where /\d5/ is rounded to the even digit. I think you want to work with the string representation of the number for this, so that machine numbers don't interfere with the rules.
By handling the special case as a string before the numeric round, we avoid binary-meets-decimal conflicts.# handle 5 in the thousandth's place with nothing or zeros following $pete =~ s/( # substitute, start capture $1 \d* # none or more digits \. # a decimal point \d # tenths digit (\d) # capture hundredths digit as $2 ) # end capture $1 50*$ # digit 5 followed by none or more 0's to the end /1&$2 ? $1+.01 : $1/xe; # then do as you had $rooker = sprintf "%.2f", $pete;
After Compline,
Zaxo
In reply to Re: A simple rounding question, but can't get it!
by Zaxo
in thread A simple rounding question, but can't get it!
by DippinPete
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |