Is there some way to really round a number to a given number of digits
after the point?
For example (1 digit):
12.3 -- 12.3
12.34 -- 12.3
12.35 -- 12.4
12.349 -- 12.3
sprintf doesn't seem to work:
$ perl -e '$a=11.45; $a=sprintf(%.1f, $a); print $a;'
Jorge Almeida [JA], on Tuesday, July 12, 2005 at 08:26 (+0100 (WEST))
typed:
JA Is there some way to really round a number to a given number of digits
JA after the point?
JA For example (1 digit):
JA 12.3 -- 12.3
JA 12.34 -- 12.3
JA 12.35 -- 12.4
JA 12.349 -- 12.3
On Tue, 12 Jul 2005, Ing. Branislav Gerzo wrote:
we should read:
perldoc -f round
interesting part:
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
Jorge Almeida [JA], on Tuesday, July 12, 2005 at 09:23 (+0100 (WEST))
thoughtfully wrote the following:
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
On Tue, 12 Jul 2005, Ing. Branislav Gerzo wrote:
what about this:
my @numbers = (12.3, 12.34, 12.35, 12.349, 11.45, 11.46);
for (@numbers) {
print $_ = . round($_, 1) . \n;
}
sub round {
my ($number,$decimals) = @_;
return substr($number+(0. . 0 x $decimals . 5),
Have a look at cpan. There's a module for that so you don't have to
implement that yourself.
math::somethin can't remember.
--manfred
Am 12.07.2005, 10:23 Uhr, schrieb Jorge Almeida [EMAIL PROTECTED]:
On Tue, 12 Jul 2005, Ing. Branislav Gerzo wrote:
we should read:
perldoc -f round
On Jul 12, Jorge Almeida said:
Is there some way to really round a number to a given number of digits
after the point?
sprintf doesn't seem to work:
$ perl -e '$a=11.45; $a=sprintf(%.1f, $a); print $a;'
11.4
From what you learned in math class, sprintf() does it wrong in
Jorge Almeida am Dienstag, 12. Juli 2005 09.26:
Is there some way to really round a number to a given number of digits
after the point?
Just as a sidenote to the help provided by others:
In some cases, the usual rounding of a ending 5 upwards
(3.5 = 4, 3.45 = 3.5 etc.)
can give an unwanted
John == John Doe [EMAIL PROTECTED] writes:
John In those cases, a rounding up- or downwards with a probability of 0.5
each can
John be appropriate, f.e.
John 3.45 = sometimes 3.4, sometimes 3.5
And that's exactly what Perl does.
However *also* keep in mind that 0.1 is not precisely 0.1 (in
Thanks to everyone for the help provided.
I already have two working solutions. :)
--
Jorge Almeida
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response
10 matches
Mail list logo