如何在Perl中绕过浮点数?

时间:2022-12-31 23:20:55

How can I round a decimal number (floating point) to the nearest integer?

如何将十进制数(浮点数)转到最近的整数?

e.g.

如。

1.2 = 1
1.7 = 2

12 个解决方案

#1


183  

Output of perldoc -q round

perldoc -q圆的输出。

Does Perl have a round() function? What about ceil() and floor()? Trig functions?

Remember that int() merely truncates toward 0. For rounding to a certain number of digits, sprintf() or printf() is usually the easiest route.

记住,int()只是将其截断为0。对于舍入一定数量的数字,sprintf()或printf()通常是最简单的方法。

    printf("%.3f", 3.1415926535);       # prints 3.142

The POSIX module (part of the standard Perl distribution) implements ceil(), floor(), and a number of other mathematical and trigonometric functions.

POSIX模块(标准Perl分布的一部分)实现了ceil()、floor()和许多其他的数学和三角函数。

    use POSIX;
    $ceil   = ceil(3.5);                        # 4
    $floor  = floor(3.5);                       # 3

In 5.000 to 5.003 perls, trigonometry was done in the Math::Complex module. With 5.004, the Math::Trig module (part of the standard Perl distribution) implements the trigonometric functions. Internally it uses the Math::Complex module and some functions can break out from the real axis into the complex plane, for example the inverse sine of 2.

在5000到5.003的perls中,三角学是在数学中完成的::复杂模块。使用5.004,Math::Trig模块(标准Perl分布的一部分)实现了三角函数。在内部,它使用数学::复杂的模块和一些函数可以从实轴分裂成复杂的平面,例如反正弦函数2。

Rounding in financial applications can have serious implications, and the rounding method used should be specified precisely. In these cases, it probably pays not to trust whichever system rounding is being used by Perl, but to instead implement the rounding function you need yourself.

在金融应用中舍入可能会有严重的影响,而使用的舍入方法应该精确地指定。在这些情况下,您可能不相信Perl使用的是舍入的系统,而是实现您自己需要的舍入函数。

To see why, notice how you'll still have an issue on half-way-point alternation:

要知道为什么,请注意你仍然会有一个关于中途改变的问题:

    for ($i = 0; $i < 1.01; $i += 0.05) { printf "%.1f ",$i}

    0.0 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.4 0.5 0.5 0.6 0.7 0.7
    0.8 0.8 0.9 0.9 1.0 1.0

Don't blame Perl. It's the same as in C. IEEE says we have to do this. Perl numbers whose absolute values are integers under 2**31 (on 32 bit machines) will work pretty much like mathematical integers. Other numbers are not guaranteed.

不要责怪Perl。这和c。IEEE说的一样,我们必须这样做。在2**31(在32位机器上)中绝对值为整数的Perl数字将非常类似于数学整数。其他数字没有保证。

#2


121  

Whilst not disagreeing with the complex answers about half-way marks and so on, for the more common (and possibly trivial) use-case:

同时,对于更常见的(也可能是琐碎的)用例,并不是不同意复杂的答案。

my $rounded = int($float + 0.5);

我的$整数= int($float + 0.5);

UPDATE

更新

If it's possible for your $float to be negative, the following variation will produce the correct result:

如果你的浮动是负数,下面的变化将产生正确的结果:

my $rounded = int($float + $float/abs($float*2 || 1));

我的$ = int($float + $float/abs($float*2 || 1));

With this calculation -1.4 is rounded to -1, and -1.6 to -2, and zero won't explode.

在这个计算中,-1.4为-1,-1.6到-2,0不会爆炸。

#3


66  

You can either use a module like Math::Round:

你可以使用数学这样的模块::

use Math::Round;
my $rounded = round( $float );

Or you can do it the crude way:

或者你可以用粗略的方法:

my $rounded = sprintf "%.0f", $float;

#4


43  

If you decide to use printf or sprintf, note that they use the Round half to even method.

如果您决定使用printf或sprintf,请注意它们使用了Round half甚至方法。

foreach my $i ( 0.5, 1.5, 2.5, 3.5 ) {
    printf "$i -> %.0f\n", $i;
}
__END__
0.5 -> 0
1.5 -> 2
2.5 -> 2
3.5 -> 4

#5


8  

See perldoc/perlfaq:

看到perldoc /将:

Remember that int() merely truncates toward 0. For rounding to a certain number of digits, sprintf() or printf() is usually the easiest route.

记住,int()只是将其截断为0。对于舍入一定数量的数字,sprintf()或printf()通常是最简单的方法。

 printf("%.3f",3.1415926535);
 # prints 3.142

The POSIX module (part of the standard Perl distribution) implements ceil(), floor(), and a number of other mathematical and trigonometric functions.

POSIX模块(标准Perl分布的一部分)实现了ceil()、floor()和许多其他的数学和三角函数。

use POSIX;
$ceil  = ceil(3.5); # 4
$floor = floor(3.5); # 3

In 5.000 to 5.003 perls, trigonometry was done in the Math::Complex module.

在5000到5.003的perls中,三角学是在数学中完成的::复杂模块。

With 5.004, the Math::Trig module (part of the standard Perl distribution) > implements the trigonometric functions.

使用5.004,Math::Trig模块(标准Perl分布的一部分)>实现了三角函数。

Internally it uses the Math::Complex module and some functions can break out from the real axis into the complex plane, for example the inverse sine of 2.

在内部,它使用数学::复杂的模块和一些函数可以从实轴分裂成复杂的平面,例如反正弦函数2。

Rounding in financial applications can have serious implications, and the rounding method used should be specified precisely. In these cases, it probably pays not to trust whichever system rounding is being used by Perl, but to instead implement the rounding function you need yourself.

在金融应用中舍入可能会有严重的影响,而使用的舍入方法应该精确地指定。在这些情况下,您可能不相信Perl使用的是舍入的系统,而是实现您自己需要的舍入函数。

To see why, notice how you'll still have an issue on half-way-point alternation:

要知道为什么,请注意你仍然会有一个关于中途改变的问题:

for ($i = 0; $i < 1.01; $i += 0.05)
{
   printf "%.1f ",$i
}

0.0 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.4 0.5 0.5 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1.0 1.0

Don't blame Perl. It's the same as in C. IEEE says we have to do this. Perl numbers whose absolute values are integers under 2**31 (on 32 bit machines) will work pretty much like mathematical integers. Other numbers are not guaranteed.

不要责怪Perl。这和c。IEEE说的一样,我们必须这样做。在2**31(在32位机器上)中绝对值为整数的Perl数字将非常类似于数学整数。其他数字没有保证。

#6


3  

You don't need any external module.

您不需要任何外部模块。

$x[0] = 1.2;
$x[1] = 1.7;

foreach (@x){
  print $_.' = '.( ( ($_-int($_))<0.5) ? int($_) : int($_)+1 );
  print "\n";
}

I may be missing your point, but I thought this was much cleaner way to do the same job.

我可能没理解你的意思,但我认为这是一种更清洁的方式来做同样的工作。

What this does is to walk through every positive number in the element, print the number and rounded integer in the format you mentioned. The code concatenates respective rounded positive integer only based on the decimals. int($_) basically round-down the number so ($-int($)) captures the decimals. If the decimals are (by definition) strictly less than 0.5, round-down the number. If not, round-up by adding 1.

它所做的是遍历元素中的每个正数,用您提到的格式打印数字和整数。该代码仅基于小数,将各自的整数整数连接起来。int($_)基本上是整数的整数($-int($))捕获小数。如果小数部分(根据定义)严格小于0.5,则将数字按整数计算。如果没有,则增加1。

#7


1  

The following will round positive or negative numbers to a given decimal position:

下面将把正或负的数转到一个给定的小数位置:

sub round ()
{
    my ($x, $pow10) = @_;
    my $a = 10 ** $pow10;

    return (int($x / $a + (($x < 0) ? -0.5 : 0.5)) * $a);
}

#8


1  

Following is a sample of five different ways to summate values. The first is a naive way to perform the summation (and fails). The second attempts to use sprintf(), but it too fails. The third uses sprintf() successfully while the final two (4th & 5th) use floor($value + 0.5).

下面是五种不同的求和方法的例子。第一个是执行求和(和失败)的简单方法。第二次尝试使用sprintf(),但它也失败了。第三个使用sprintf()成功,而最后两个(第4和第5)使用层($value + 0.5)。

 use strict;
 use warnings;
 use POSIX;

 my @values = (26.67,62.51,62.51,62.51,68.82,79.39,79.39);
 my $total1 = 0.00;
 my $total2 = 0;
 my $total3 = 0;
 my $total4 = 0.00;
 my $total5 = 0;
 my $value1;
 my $value2;
 my $value3;
 my $value4;
 my $value5;

 foreach $value1 (@values)
 {
      $value2 = $value1;
      $value3 = $value1;
      $value4 = $value1;
      $value5 = $value1;

      $total1 += $value1;

      $total2 += sprintf('%d', $value2 * 100);

      $value3 = sprintf('%1.2f', $value3);
      $value3 =~ s/\.//;
      $total3 += $value3;

      $total4 += $value4;

      $total5 += floor(($value5 * 100.0) + 0.5);
 }

 $total1 *= 100;
 $total4 = floor(($total4 * 100.0) + 0.5);

 print '$total1: '.sprintf('%011d', $total1)."\n";
 print '$total2: '.sprintf('%011d', $total2)."\n";
 print '$total3: '.sprintf('%011d', $total3)."\n";
 print '$total4: '.sprintf('%011d', $total4)."\n";
 print '$total5: '.sprintf('%011d', $total5)."\n";

 exit(0);

 #$total1: 00000044179
 #$total2: 00000044179
 #$total3: 00000044180
 #$total4: 00000044180
 #$total5: 00000044180

Note that floor($value + 0.5) can be replaced with int($value + 0.5) to remove the dependency on POSIX.

注意,可以用int($value + 0.5)替换地板($value + 0.5),以消除对POSIX的依赖。

#9


1  

Negative numbers can add some quirks that people need to be aware of.

负数可以添加一些人们需要注意的怪癖。

printf-style approaches give us correct numbers, but they can result in some odd displays. We have discovered that this method (in my opinion, stupidly) puts in a - sign whether or not it should or shouldn't. For example, -0.01 rounded to one decimal place returns a -0.0, rather than just 0. If you are going to do the printf style approach, and you know you want no decimal, use %d and not %f (when you need decimals, it's when the display gets wonky).

printf风格的方法给了我们正确的数字,但是它们会导致一些奇怪的显示。我们已经发现这种方法(在我看来,愚蠢地)放入一个-符号,不管它是否应该或不应该。例如,-0.01四舍五入到一个小数点后,返回-0.0,而不是0。如果您要执行printf样式方法,并且您知道您不需要小数,那么使用%d,而不是%f(当您需要小数时,当显示变得不稳定时)。

While it's correct and for math no big deal, for display it just looks weird showing something like "-0.0".

虽然这是正确的,但对于数学来说没什么大不了的,因为显示它看起来有点怪异,比如“-0.0”。

For the int method, negative numbers can change what you want as a result (though there are some arguments that can be made they are correct).

对于int方法,负数可以改变您想要的结果(尽管有一些参数可以使它们是正确的)。

The int + 0.5 causes real issues with -negative numbers, unless you want it to work that way, but I imagine most people don't. -0.9 should probably round to -1, not 0. If you know that you want negative to be a ceiling rather than a floor then you can do it in one-liner, otherwise, you might want to use the int method with a minor modification (this obviously only works to get back whole numbers:

int + 0.5导致了实际问题的负数,除非你想让它那样工作,但我想大多数人不会这样做。-0。9应该是-1,而不是0。如果你知道你想要的是一个天花板而不是一个地板,那么你可以用一个线性的方法来做,否则,你可能会想用一个小的修改来使用int方法(这显然只是为了得到全部的数字:

my $var = -9.1;
my $tmpRounded = int( abs($var) + 0.5));
my $finalRounded = $var >= 0 ? 0 + $tmpRounded : 0 - $tmpRounded;

#10


0  

My solution for sprintf

我的解决方案sprintf

if ($value =~ m/\d\..*5$/){
    $format =~ /.*(\d)f$/;
    if (defined $1){
       my $coef = "0." . "0" x $1 . "05";    
            $value = $value + $coef;    
    }
}

$value = sprintf( "$format", $value );

#11


0  

If you are only concerned with getting an integer value out of a whole floating point number (i.e. 12347.9999 or 54321.0001), this approach (borrowed and modified from above) will do the trick:

如果您只关心从整个浮点数(即12347.9999或54321.0001)中获得一个整数值,那么这个方法(从上面借用和修改)将会达到这个目的:

my $rounded = floor($float + 0.1); 

#12


-2  

cat table |
  perl -ne '/\d+\s+(\d+)\s+(\S+)/ && print "".**int**(log($1)/log(2))."\t$2\n";' 

#1


183  

Output of perldoc -q round

perldoc -q圆的输出。

Does Perl have a round() function? What about ceil() and floor()? Trig functions?

Remember that int() merely truncates toward 0. For rounding to a certain number of digits, sprintf() or printf() is usually the easiest route.

记住,int()只是将其截断为0。对于舍入一定数量的数字,sprintf()或printf()通常是最简单的方法。

    printf("%.3f", 3.1415926535);       # prints 3.142

The POSIX module (part of the standard Perl distribution) implements ceil(), floor(), and a number of other mathematical and trigonometric functions.

POSIX模块(标准Perl分布的一部分)实现了ceil()、floor()和许多其他的数学和三角函数。

    use POSIX;
    $ceil   = ceil(3.5);                        # 4
    $floor  = floor(3.5);                       # 3

In 5.000 to 5.003 perls, trigonometry was done in the Math::Complex module. With 5.004, the Math::Trig module (part of the standard Perl distribution) implements the trigonometric functions. Internally it uses the Math::Complex module and some functions can break out from the real axis into the complex plane, for example the inverse sine of 2.

在5000到5.003的perls中,三角学是在数学中完成的::复杂模块。使用5.004,Math::Trig模块(标准Perl分布的一部分)实现了三角函数。在内部,它使用数学::复杂的模块和一些函数可以从实轴分裂成复杂的平面,例如反正弦函数2。

Rounding in financial applications can have serious implications, and the rounding method used should be specified precisely. In these cases, it probably pays not to trust whichever system rounding is being used by Perl, but to instead implement the rounding function you need yourself.

在金融应用中舍入可能会有严重的影响,而使用的舍入方法应该精确地指定。在这些情况下,您可能不相信Perl使用的是舍入的系统,而是实现您自己需要的舍入函数。

To see why, notice how you'll still have an issue on half-way-point alternation:

要知道为什么,请注意你仍然会有一个关于中途改变的问题:

    for ($i = 0; $i < 1.01; $i += 0.05) { printf "%.1f ",$i}

    0.0 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.4 0.5 0.5 0.6 0.7 0.7
    0.8 0.8 0.9 0.9 1.0 1.0

Don't blame Perl. It's the same as in C. IEEE says we have to do this. Perl numbers whose absolute values are integers under 2**31 (on 32 bit machines) will work pretty much like mathematical integers. Other numbers are not guaranteed.

不要责怪Perl。这和c。IEEE说的一样,我们必须这样做。在2**31(在32位机器上)中绝对值为整数的Perl数字将非常类似于数学整数。其他数字没有保证。

#2


121  

Whilst not disagreeing with the complex answers about half-way marks and so on, for the more common (and possibly trivial) use-case:

同时,对于更常见的(也可能是琐碎的)用例,并不是不同意复杂的答案。

my $rounded = int($float + 0.5);

我的$整数= int($float + 0.5);

UPDATE

更新

If it's possible for your $float to be negative, the following variation will produce the correct result:

如果你的浮动是负数,下面的变化将产生正确的结果:

my $rounded = int($float + $float/abs($float*2 || 1));

我的$ = int($float + $float/abs($float*2 || 1));

With this calculation -1.4 is rounded to -1, and -1.6 to -2, and zero won't explode.

在这个计算中,-1.4为-1,-1.6到-2,0不会爆炸。

#3


66  

You can either use a module like Math::Round:

你可以使用数学这样的模块::

use Math::Round;
my $rounded = round( $float );

Or you can do it the crude way:

或者你可以用粗略的方法:

my $rounded = sprintf "%.0f", $float;

#4


43  

If you decide to use printf or sprintf, note that they use the Round half to even method.

如果您决定使用printf或sprintf,请注意它们使用了Round half甚至方法。

foreach my $i ( 0.5, 1.5, 2.5, 3.5 ) {
    printf "$i -> %.0f\n", $i;
}
__END__
0.5 -> 0
1.5 -> 2
2.5 -> 2
3.5 -> 4

#5


8  

See perldoc/perlfaq:

看到perldoc /将:

Remember that int() merely truncates toward 0. For rounding to a certain number of digits, sprintf() or printf() is usually the easiest route.

记住,int()只是将其截断为0。对于舍入一定数量的数字,sprintf()或printf()通常是最简单的方法。

 printf("%.3f",3.1415926535);
 # prints 3.142

The POSIX module (part of the standard Perl distribution) implements ceil(), floor(), and a number of other mathematical and trigonometric functions.

POSIX模块(标准Perl分布的一部分)实现了ceil()、floor()和许多其他的数学和三角函数。

use POSIX;
$ceil  = ceil(3.5); # 4
$floor = floor(3.5); # 3

In 5.000 to 5.003 perls, trigonometry was done in the Math::Complex module.

在5000到5.003的perls中,三角学是在数学中完成的::复杂模块。

With 5.004, the Math::Trig module (part of the standard Perl distribution) > implements the trigonometric functions.

使用5.004,Math::Trig模块(标准Perl分布的一部分)>实现了三角函数。

Internally it uses the Math::Complex module and some functions can break out from the real axis into the complex plane, for example the inverse sine of 2.

在内部,它使用数学::复杂的模块和一些函数可以从实轴分裂成复杂的平面,例如反正弦函数2。

Rounding in financial applications can have serious implications, and the rounding method used should be specified precisely. In these cases, it probably pays not to trust whichever system rounding is being used by Perl, but to instead implement the rounding function you need yourself.

在金融应用中舍入可能会有严重的影响,而使用的舍入方法应该精确地指定。在这些情况下,您可能不相信Perl使用的是舍入的系统,而是实现您自己需要的舍入函数。

To see why, notice how you'll still have an issue on half-way-point alternation:

要知道为什么,请注意你仍然会有一个关于中途改变的问题:

for ($i = 0; $i < 1.01; $i += 0.05)
{
   printf "%.1f ",$i
}

0.0 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.4 0.5 0.5 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1.0 1.0

Don't blame Perl. It's the same as in C. IEEE says we have to do this. Perl numbers whose absolute values are integers under 2**31 (on 32 bit machines) will work pretty much like mathematical integers. Other numbers are not guaranteed.

不要责怪Perl。这和c。IEEE说的一样,我们必须这样做。在2**31(在32位机器上)中绝对值为整数的Perl数字将非常类似于数学整数。其他数字没有保证。

#6


3  

You don't need any external module.

您不需要任何外部模块。

$x[0] = 1.2;
$x[1] = 1.7;

foreach (@x){
  print $_.' = '.( ( ($_-int($_))<0.5) ? int($_) : int($_)+1 );
  print "\n";
}

I may be missing your point, but I thought this was much cleaner way to do the same job.

我可能没理解你的意思,但我认为这是一种更清洁的方式来做同样的工作。

What this does is to walk through every positive number in the element, print the number and rounded integer in the format you mentioned. The code concatenates respective rounded positive integer only based on the decimals. int($_) basically round-down the number so ($-int($)) captures the decimals. If the decimals are (by definition) strictly less than 0.5, round-down the number. If not, round-up by adding 1.

它所做的是遍历元素中的每个正数,用您提到的格式打印数字和整数。该代码仅基于小数,将各自的整数整数连接起来。int($_)基本上是整数的整数($-int($))捕获小数。如果小数部分(根据定义)严格小于0.5,则将数字按整数计算。如果没有,则增加1。

#7


1  

The following will round positive or negative numbers to a given decimal position:

下面将把正或负的数转到一个给定的小数位置:

sub round ()
{
    my ($x, $pow10) = @_;
    my $a = 10 ** $pow10;

    return (int($x / $a + (($x < 0) ? -0.5 : 0.5)) * $a);
}

#8


1  

Following is a sample of five different ways to summate values. The first is a naive way to perform the summation (and fails). The second attempts to use sprintf(), but it too fails. The third uses sprintf() successfully while the final two (4th & 5th) use floor($value + 0.5).

下面是五种不同的求和方法的例子。第一个是执行求和(和失败)的简单方法。第二次尝试使用sprintf(),但它也失败了。第三个使用sprintf()成功,而最后两个(第4和第5)使用层($value + 0.5)。

 use strict;
 use warnings;
 use POSIX;

 my @values = (26.67,62.51,62.51,62.51,68.82,79.39,79.39);
 my $total1 = 0.00;
 my $total2 = 0;
 my $total3 = 0;
 my $total4 = 0.00;
 my $total5 = 0;
 my $value1;
 my $value2;
 my $value3;
 my $value4;
 my $value5;

 foreach $value1 (@values)
 {
      $value2 = $value1;
      $value3 = $value1;
      $value4 = $value1;
      $value5 = $value1;

      $total1 += $value1;

      $total2 += sprintf('%d', $value2 * 100);

      $value3 = sprintf('%1.2f', $value3);
      $value3 =~ s/\.//;
      $total3 += $value3;

      $total4 += $value4;

      $total5 += floor(($value5 * 100.0) + 0.5);
 }

 $total1 *= 100;
 $total4 = floor(($total4 * 100.0) + 0.5);

 print '$total1: '.sprintf('%011d', $total1)."\n";
 print '$total2: '.sprintf('%011d', $total2)."\n";
 print '$total3: '.sprintf('%011d', $total3)."\n";
 print '$total4: '.sprintf('%011d', $total4)."\n";
 print '$total5: '.sprintf('%011d', $total5)."\n";

 exit(0);

 #$total1: 00000044179
 #$total2: 00000044179
 #$total3: 00000044180
 #$total4: 00000044180
 #$total5: 00000044180

Note that floor($value + 0.5) can be replaced with int($value + 0.5) to remove the dependency on POSIX.

注意,可以用int($value + 0.5)替换地板($value + 0.5),以消除对POSIX的依赖。

#9


1  

Negative numbers can add some quirks that people need to be aware of.

负数可以添加一些人们需要注意的怪癖。

printf-style approaches give us correct numbers, but they can result in some odd displays. We have discovered that this method (in my opinion, stupidly) puts in a - sign whether or not it should or shouldn't. For example, -0.01 rounded to one decimal place returns a -0.0, rather than just 0. If you are going to do the printf style approach, and you know you want no decimal, use %d and not %f (when you need decimals, it's when the display gets wonky).

printf风格的方法给了我们正确的数字,但是它们会导致一些奇怪的显示。我们已经发现这种方法(在我看来,愚蠢地)放入一个-符号,不管它是否应该或不应该。例如,-0.01四舍五入到一个小数点后,返回-0.0,而不是0。如果您要执行printf样式方法,并且您知道您不需要小数,那么使用%d,而不是%f(当您需要小数时,当显示变得不稳定时)。

While it's correct and for math no big deal, for display it just looks weird showing something like "-0.0".

虽然这是正确的,但对于数学来说没什么大不了的,因为显示它看起来有点怪异,比如“-0.0”。

For the int method, negative numbers can change what you want as a result (though there are some arguments that can be made they are correct).

对于int方法,负数可以改变您想要的结果(尽管有一些参数可以使它们是正确的)。

The int + 0.5 causes real issues with -negative numbers, unless you want it to work that way, but I imagine most people don't. -0.9 should probably round to -1, not 0. If you know that you want negative to be a ceiling rather than a floor then you can do it in one-liner, otherwise, you might want to use the int method with a minor modification (this obviously only works to get back whole numbers:

int + 0.5导致了实际问题的负数,除非你想让它那样工作,但我想大多数人不会这样做。-0。9应该是-1,而不是0。如果你知道你想要的是一个天花板而不是一个地板,那么你可以用一个线性的方法来做,否则,你可能会想用一个小的修改来使用int方法(这显然只是为了得到全部的数字:

my $var = -9.1;
my $tmpRounded = int( abs($var) + 0.5));
my $finalRounded = $var >= 0 ? 0 + $tmpRounded : 0 - $tmpRounded;

#10


0  

My solution for sprintf

我的解决方案sprintf

if ($value =~ m/\d\..*5$/){
    $format =~ /.*(\d)f$/;
    if (defined $1){
       my $coef = "0." . "0" x $1 . "05";    
            $value = $value + $coef;    
    }
}

$value = sprintf( "$format", $value );

#11


0  

If you are only concerned with getting an integer value out of a whole floating point number (i.e. 12347.9999 or 54321.0001), this approach (borrowed and modified from above) will do the trick:

如果您只关心从整个浮点数(即12347.9999或54321.0001)中获得一个整数值,那么这个方法(从上面借用和修改)将会达到这个目的:

my $rounded = floor($float + 0.1); 

#12


-2  

cat table |
  perl -ne '/\d+\s+(\d+)\s+(\S+)/ && print "".**int**(log($1)/log(2))."\t$2\n";'