Truncate (not rounded) decimals in sprintf?

I want to show the dollar value with two digits after the decimal point to indicate cents. In the program below, the output is 23.24. Perl rounds decimals. How to avoid this. I want the result to be 23.23.

$val=23.2395;
$testa=sprintf("%.2f", $val);
print "\n$testa\n $val";
+5
source share
2 answers
print int(23.2395*100)/100;  # => 23.23
+12
source

Math :: Round has different rounding methods.

use Math::Round 'nlowmult';
print nlowmult( 0.01, 23.2395 ); # 23.23
+7
source

All Articles