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

Sorry to be dim but, with the following lines of code: $_ = FormatSigFigs($_,8); my $clem = sprintf("%08g", $_); print "$clem\n"; and starting with the number 1298.844667, I get the result 01298.84 and not 1298.845, as desired. Any ideas? Thanks again for your help
  • Comment on Re: Rounding to a Given Number of Significant Figures Rather Than Decimal Places

Replies are listed 'Best First'.
Re: Re: Rounding to a Given Number of Significant Figures Rather Than Decimal Places
by bart (Canon) on Jan 14, 2003 at 11:20 UTC
    Drop the zero in the format.

    Update: Urm... that will replace the leading zero with a space, which is only part of your problem... Apparently, the default precision offered by "%g" is smaller than the precision you desire.

    OK, try this:

    $_ = 1298.844667; my $clem = sprintf("%.7g", $_); print "$clem\n";
    Result:
    1298.845
    OK?