How do I set the floating point precision in Perl?

前端 未结 5 1985
失恋的感觉
失恋的感觉 2020-12-18 20:54

Is there a way to set a Perl script\'s floating point precision (to 3 digits), without having to change it specifically for every variable?

Something similar to TCL\

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-18 21:41

    There is no way to globally change this.

    If it is just for display purposes then use sprintf("%.3f", $value);.

    For mathematical purposes, use (int(($value * 1000.0) + 0.5) / 1000.0). This would work for positive numbers. You would need to change it to work with negative numbers though.

提交回复
热议问题