Re: Significant Figures

From: Steve Cosner <stevec_at_zimmer.CSUFresno.EDU>
Date: 1996/07/03
Message-ID: <4rf3pd$ipu_at_zimmer.CSUFresno.EDU>#1/1


In article <31D9FA54.B3_at_csus.edu>, Mitchell Ryan <ryanmr_at_csus.edu> wrote:
>Mitchell Ryan wrote:
>>
>> Does any know of an Oracle function that handles significant figures?
>> Or does anyone know of an function that someone has written that I
>> can download that does this?
 

>What I mean by this is that I am looking for a function that I can
>specify I want, say, two significant figures, and Oracle function will
>evaluate the number in the column and return, two significant figures.
>By example: if I'm given a number like 12.043, and I specify 2
>significant figures than I want returned 12.
>
>If I wanted 3 significant figures the number returned would be 12.04
>(the zero is the place holder in this case).

You will have to write something yourself. The only problem I see is your interpretation of the zero in your 12.04 example. 12.04 looks like 4 significant figures to me. Three significant figures would be 12.0.

Here's a stab at a function, using *my* interpretation of significant figures:

Function SigFigs(numb in number, figs in number) return number is   num_string varchar2(30);
  decimal_pnt number;
  new_string varchar2(30);
Begin

  • strip off leading and trailing zeros -- Num_string := ltrim(to_char(numb));
  • locate the decimal point -- decimal_pnt := instr(num_string,'.');
  • Check if the number is already short enough -- if decimal_pnt=0 and length(num_string) <= figs or decimal_pnt=1 and length(num_string) <= figs+1 then return numb; end if;
  • Integer numbers and numbers where decimal part can be dropped
  • Change 12345 or 12345.678 to 12300 if decimal_pnt = 0 or decimal_pnt >= figs then new_string := substr(num_string,1,figs) || '000000000000'; new_string := substr(new_string,1,length(num_string)); return to_number(new_string); else
  • numbers with decimal points --
  • Change 123.4567 to 123.4 return to_number(substr(num_string,1,figs+1); end if; end;

If your numbers can be negative then the function needs to account for the negative sign, too. This function truncates, and does not round anything. If you want to round things, it becomes a bit more complex.

This is all untested junk from the top of my head, so beware...you get what you pay for. ;-) In the meantime, somebody else will probably come up with an incredibly simple function for you...

HTH,
Steve Cosner Received on Wed Jul 03 1996 - 00:00:00 CEST

Original text of this message