向下舍入到Perl中的不同小数位数

Ram*_*ino 1 floating-point perl

采用浮点数,我想向下舍入,具体取决于用户定义的"bin"大小.因此,容器大小将根据用户偏好而改变.例如,箱尺寸可以是0.5,0.1,1或甚至0.01.

我的目标是确定浮点数落入哪个bin.举个例子:

0.1箱尺寸:

2348.285 will fall into a 2348.2 bin

238.592 will fall into a 238.5 bin
Run Code Online (Sandbox Code Playgroud)

0.5箱尺寸:

2348.285 will fall into a 2348.0 bin

238.592 will fall into a 238.5 bin
Run Code Online (Sandbox Code Playgroud)

0.01箱尺寸:

2348.285 will fall into a 2348.28 bin

238.592 will fall into a 238.59 bin
Run Code Online (Sandbox Code Playgroud)

一箱尺寸:

2348.285 will fall into a 2348 bin

238.592 will fall into a 238 bin
Run Code Online (Sandbox Code Playgroud)

我已经调查在Perl四舍五入例如,地板(),sprintf的(),也SUBSTR()方法,但是,没有人做了很多想我想要做的,或者说,我不能让他们做什么,我想.如果给定用户定义的bin大小,那么整齐的代码会动态地改变浮点数向下舍入的最低值?

小智 5

您可以尝试Math :: Round:

use Math::Round 'nlowmult';

print nlowmult( .01, 2348.285 ) . "\n";
print nlowmult( .01, 238.592 ) . "\n";
Run Code Online (Sandbox Code Playgroud)