in reply to number comparison with a twist
I'm expanding veltro's suggestion to use the string representation, which smoothes the rounding errors from floats away.
DB<42> $num1 = 1990; $num2 = '19.90' DB<43> p $num1 == ("" . $num2*100) # number->to_string->to_n +umber 1 DB<44>
This test proves it works reliably even with tenth of cents
DB<41> say join "\n", grep { sprintf ("%03d",$_) != ("".($_/1000)*100 +0) } 0..99999 DB<42>
Please note that while this seems dirty, a numeric comparison with == will always do what you want even if the formats are not like expected.
Like having leading zeroes or not exactly 2 decimal points.
DB<44> $num1 = '001990'; $num2 = '19.9' DB<45> p $num1 == ("" . $num2*100) + 1 DB<46> $num2 = '19.900' DB<47> p $num1 == ("" . $num2*100) 1 DB<60> $num1 = '199e1' DB<61> p $num1 == ("" . $num2*100) 1
an easier notation might be
DB<63> $num2*=100 DB<64> p $num1 == "$num2" 1 DB<65>
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
|
|---|