How do I set the floating point precision in Perl?

前端 未结 5 1966
失恋的感觉
失恋的感觉 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:33

    I wouldn't recommend to use sprintf("%.3f", $value).

    Please look at the following example: (6.02*1.25 = 7.525)

    printf("%.2f", 6.02 * 1.25) = 7.52

    printf("%.2f", 7.525) = 7.53

提交回复
热议问题