Re: significant figures



On Tue, 05 Jan 2010 22:38:41 +0100, dan <spam.meplease@xxxxxxxxxxxx> wrote:

Whilst trying to create something that would parse a number into one with
an appropriate number of significant figures, I accidentally wrote this:

sub sigfig {
my ($sigfigs, $number) = @_;

$number = sprintf("%d",$number); # just in case...
return $number if if ($sigfgs > length($number);

my $divisor = 10**(length(int $number) - $sigfigs);
$number /= $divisor;
$number = sprintf "%1.0f", $number;

# instead:
$number = sprintf("%d",$number);

$number *= $divisor;

return $number
}

inadequacies,foibles and associated notwithstandings of the above code,

Well, if it works, it works.

A bit shorter:

$number = sprintf("%d",$number); # just in case...
return substr($number,0,$sigfigs) . ("0" x (length($number) - $sigfigs));

.