Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate (not round) decimal places in sprintf?

I want to display the dollar value with two digits after the decimal point to denote the cents. In the below program the output is 23.24. Perl rounds the decimal places. How to avoid it. I want the output to be 23.23.

$val=23.2395;
$testa=sprintf("%.2f", $val);
print "\n$testa\n $val";
like image 331
Arav Avatar asked Mar 20 '12 05:03

Arav


1 Answers

print int(23.2395*100)/100;  # => 23.23
like image 73
dbenhur Avatar answered Sep 30 '22 14:09

dbenhur