Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Significant Digits and Rounding

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
Does anyone have a function (or is able to suggest how i can construct one) which I pass a value and the return is the value to an indicated number of significant figures (with the last digit being rounded up/down accordingly).
For instance
19999 to 3 sf rounded down --> 19900
19999 to 3 sf rounded up --> 20000
12999 to 2 sf rounded down --> 12900
12999 to 2 sf rounded up --> 13000
1.23456 to 4 sf rounded up --> 1.235
1.23456 to 4 sf rounded down --> 1.234
Any ideas ?
Steve
 
Here's what I was able to code to improve the Delphi Round() function -

function RndSig ( Number : Extended; Sig : Word; Direction : Boolean ) : Extended;
var
Whole : Boolean;
begin
if ( Number = int( Number ) ) then
Whole := True
else
Whole := False;

if ( Whole ) then
begin
Number := Number / power( 10, Sig - 1 );
{ Round Up }
if ( Direction ) then
Number := round( Number + 0.5 )
{ Round Down }
else
Number := round( Number - 0.5 );

Result := Number * power( 10, Sig - 1 );
end
else
begin
Number := Number * power( 10, Sig - 1 );
{ Round Up }
if ( Direction ) then
Number := round( Number + 0.5 )
{ Round Down }
else
Number := round( Number - 0.5 );

Result := Number / power( 10, Sig - 1 );
end;
end;


NOTE: This was done from memory. It has been a while since I coded this function and I'm unable to find the project it is in. This should be enough for you to get started though.
 
Thanks for the code example.
It looks like it might be the start of a solution for us.
Are you able to provide the full code as this doesn't quite produce the required / expected results
For instance :
RndSig(2222, 1, False) = 2222 (should be 2000 ??)
RndSig(2222, 2, False) = 2220 (should be 2200 ??)
RndSig(2222, 1, True) = 2222 (should be 3000 ??)
RndSig(2222, 2, True) = 2230 (should be 2300 ??)
Similarly for decimals I get :
RndSig(12.34, 1, False) = 12 (should be 10 ??)
RndSig(12.34, 2, False) = 1230 (should be 12 ??)
RndSig(12.34, 1, True) = 20 - Correct
RndSig(12.34, 2, True) = 1240 (should be 13 ??)

I'd be most grateful if you would be able to produce the code for me - would save me pulling out any more hair with this issue.
Feel free to post the code on the forum or email me if convenient.

Many Thanks,
Steve
 
My apologies.
I'd entered the code incorrectly for the non-whole number values.
The values I'm getting out are :
RndSig(12.34, 1, False) = 12 (should be 10 ??)
RndSig(12.34, 2, False) = 12.30 (should be 12 ??)
RndSig(12.34, 1, True) = 13 (should be 20)
RndSig(12.34, 2, True) = 12.4 (should be 13 ??)

Are you able to get the full code to me.
Steve

(steven.keclik@s3t.co.uk)
 
I'll have to look for it in my old projects this evening.

They way this rounding function was designed to work was centered around the decimal point. By this I mean that round whole numbers, the first significant number is the one's place, second is the ten's, third is the hundred's and so on. For decimal rounding, the first significant number is the tenth's, second is the hundredth's, third is the thousandth's and so on. This maybe wrong mathematically, but it sovled my problem.

Examples of this would be :
Decimals
RndSig( 12.567, 1, True ) = 12.6
RndSig( 12.567, 1, False ) = 12.5
RndSig( 12.567, 2, True ) = 12.56
RndSig( 12.567, 2, False ) = 12.57
RndSig( 12.567, 0, True ) = 13
RndSig( 12.567, 0, False ) = 12

Whole
RndSig( 3568, 2, True ) = 3560
RndSig( 3568, 2, False ) = 3560
RndSig( 3568, 3, True ) = 3600
RndSig( 3568, 3, False ) = 3500
RndSig( 3568, 4, True ) = 4000
RndSig( 3568, 4, False ) = 3000
 
Big mistake with the RndSig function!!

The lines that read - Number := round( Number + 0.5 ) and
Number := round( Number - 0.5 )

Should read - Number := int( Number + 0.5 ) and
Number := int( Number - 0.5 ) respectfully

Sorry, my memory isn't what it used to be.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top