gpt4 book ai didi

perl - 对 Math::BigFloat 数字进行舍入

转载 作者:行者123 更新时间:2023-12-03 09:27:46 27 4
gpt4 key购买 nike

据我了解,阅读 Math::BigFloat 的文档,以下应该是对数字进行四舍五入的代码,但它似乎不起作用。

#!/usr/bin/perl

use strict;
use warnings;
use Math::BigFloat;

my $x = Math::BigFloat->new('2.3');
$x->ffround(0, '+inf');
print "$x\n"; # -> 2

我应该怎么做才能始终将数字向上舍入,例如,在本例中获取数字3作为输出。

最佳答案

舍入模式仅影响从两个可能结果之间的中间舍入的行为:

#!/usr/bin/perl
use warnings;
use strict;

use Math::BigFloat;

my $n = Math::BigFloat->new('2.5');

print $n->copy->ffround(1, 'zero'); # 2
print $n->copy->ffround(1, '+inf'); # 3
print $n->copy->ffround(1, 'odd'); # 3
print $n->copy->ffround(1, 'even'); # 2

你想要的是bceil:

my $m = Math::BigFloat->new('2.3');

print $m->copy->bceil(); # 3

关于perl - 对 Math::BigFloat 数字进行舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16776730/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com