in reply to Re: Re: Rounding to a Given Number of Significant Figures Rather Than Decimal Places
in thread Rounding to a Given Number of Significant Figures Rather Than Decimal Places

Heh. Try doing something like:
my $num = 12345.678; # a total of 9 characters if (length $num > 8) { if ($num =~ /\./) # If there's a decimal point { # Put it to seven significant figures } else { # Put it to eight significant figures } }
Another way to test for the decimal point, if you wanted to be purely math-based, could be if ($num == round($num)).

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re4: Rounding to a Given Number of Significant Figures Rather Than Decimal Places
by Hofmator (Curate) on Jan 13, 2003 at 16:53 UTC

    What about a minus/plus sign in front of the number? And what if the number is longer than 8 characters, like 1234567890?

    I think there are a lot of corner cases lurking around in this problem ... :)

    -- Hofmator

      Hmmm ... sounds like a job for

      .

      .

      .

      Requirements-Gathering Person!

      (Or, Requirements-Revising Person, as the case may be...)

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.