|
|
Question : Truncating Floats
|
|
$PERCENT = $floatA / $floatB;
I get an answer which is in the form "X.13" decimal places. I want this to be 4 decimal places, BUT I don't want it done just when I print but have it stored that way. I am familiar with ouput formatting, but I would have to do this hundreds of times so I want to be able to specify it in the beginning.
Somewhat like how the int () function works if possible. Thank you.
|
Answer : Truncating Floats
|
|
$PERCENT = sprintf("%.4f", $floatA/$floatB);
Because of the way perl sort of merges numeric and string values. That will do it. To prove that, try
$a = 1.0; $b = 9.0;
$p = sprintf("%.4f", $a/$b); $p += 1; print "Got $p\n";
|
|
|
|
|