如何在Perl中设置浮点精度? [英] How do I set the floating point precision in Perl?

查看:188
本文介绍了如何在Perl中设置浮点精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法设置一个Perl脚本的浮点精度(3位数字),而不必为每个变量专门进行更改?



类似于TCL的:

  global tcl_precision 
set tcl_precision 3


如果是这样的话只是为了显示目的,然后使用 sprintf(%。3f,$ value);
$ b

,使用(int(($ value * 1000.0)+ 0.5)/ 1000.0)。这将工作正数。您需要将其更改为使用负数。


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's:

global tcl_precision
set tcl_precision 3

解决方案

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.

这篇关于如何在Perl中设置浮点精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆