#!/usr/bin/perl use strict; use warnings; my $s=2; for (my $i = 0; $i <= (1/10**($s-1)); $i += (5/10**($s+1))) { printf("%.".($s+1)."f --> sprintf:%.$s"."f <> round:%s <> int:%.$s"."f\n", $i, $i, round($i,$s), int($i*(10**$s)+0.5)/(10**$s) ); } sub round { my $zahl=shift; my $stelle=shift; my $zz=$zahl+0; if($zz=~/^.+?\.\d{$stelle}/) { $zz=~s/^.+?\.\d{$stelle}/'0.'.('0'x$stelle)/e; $zahl+=$zz; $zahl=~s/^(.+?\.\d{$stelle}).*$/$1/; } $zahl=sprintf("%.$stelle"."f",$zahl); return $zahl; }