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

$num = sprintf "%8g", $num;
Almost. shell$> perldoc -f sprintf reveals this little snippet:
# Format number with up to 8 leading zeroes $result = sprintf("%08d", $number);
Change the "d" to a "g" (which you had), and you'll be there. You'll probably find you want to do the SigFig rounding first, then format it for display with the sprintf.
cheers
davis
Is this going out live?
No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
  • Comment on Re: Re: Re: Rounding to a Given Number of Significant Figures Rather Than Decimal Places
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: Re: Rounding to a Given Number of Significant Figures Rather Than Decimal Places
by Clem (Sexton) on Jan 13, 2003 at 15:36 UTC
    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.
Re: Re: Re: Re: Rounding to a Given Number of Significant Figures Rather Than Decimal Places
by Clem (Sexton) on Jan 13, 2003 at 15:43 UTC
    Sorry to be a bit dim but, with the following lines of code:
    $_ = FormatSigFigs($_,8); my $clem = sprintf("%08g", $_); print "$clem\n";
    and the number 1298.844667 as input, I get the output 01298.84 and not 1298.845, which is what I want.
    Any ideas?
    Thanks again for your help.

    Edit: larsen. Added <code> tags and some minor formatting.