Board index » delphi » Rounding off a real number to half thousands

Rounding off a real number to half thousands

In article <4g77uv$...@crash.microserve.net>
           allan...@pennet.net "Allan Bremer" writes:

Quote
> Hello,

> I am trying to round off real numbers (or a string) to half
> thousands.

> example:  34.574423  to  34.5745
>            3.123164  to   3.1230
>            0.012345  to   0.0120

> You see, there rounded off to the nearest half thousand.  How can I
> do this using Pascal.

Multiply by 200, Round normally to nearest integer, divide by 200.

(I think...)

--
* TQ 1.0 * The 'Just So Quotes'.
I keep hearing that Jesus Christ is coming, but nobody knows his tour dates.
        -- Michael Lucas

 

Re:Rounding off a real number to half thousands


Hello,

I am trying to round off real numbers (or a string) to half
thousands.

example:  34.574423  to  34.5745
           3.123164  to   3.1230
           0.012345  to   0.0120

You see, there rounded off to the nearest half thousand.  How can I
do this using Pascal.

Thank you

Re:Rounding off a real number to half thousands


On 18 Feb 1996, Allan Bremer wrote:

Quote
> Hello,

> I am trying to round off real numbers (or a string) to half
> thousands.

> example:  34.574423  to  34.5745
>            3.123164  to   3.1230
>            0.012345  to   0.0120

> You see, there rounded off to the nearest half thousand.  How can I
> do this using Pascal.

If you multiply the number by 1000, you can separate the integer and decimal
part, and use the decimal part to determine if the integer part is to be
rounded to next number, to .5, or down to 0
Ex. 34.574423
   Multi. by 1000     ->  34574.423
   Get decimal        ->  dec := .423
   Get integer        ->  number := 34574
   If 0 <= dec < .5   ->  number := number       -> 34574
      else
   If .5 <= dec < .75 ->  number := number + .5  -> 34574.5
     else
   if dec >= .75      ->  number := number + 1   -> 34575
 (in this case, number will be 34574.5 because .5 <= dec < .75)
   divide by 1000     ->  34.5745

function roundnum(number:real) : real;
var
  dec : real;
begin
   number := number * 1000;
   dec := frac(number);
   number := int(number);    

   if (dec >= 0.35) and (dec < 0.75) then number := number + 0.5
      else if (dec >= 0.75) then number := number + 1;

   roundnum := number / 1000;
end;

 >     Sam Lane     <
 >  saml...@wpi.edu <

Re:Rounding off a real number to half thousands


Re:Rounding off a real number to half thousands


Quote
On Sun, 18 Feb 1996, Sam wrote:
> If you multiply the number by 1000, you can separate the integer and decimal
> part, and use the decimal part to determine if the integer part is to be
> rounded to next number, to .5, or down to 0
> Ex. 34.574423
>    Multi. by 1000     ->  34574.423
>    Get decimal        ->  dec := .423
>    Get integer        ->  number := 34574
>*    If 0 <= dec < .25   ->  number := number       -> 34574
>       else
>*    If .25 <= dec < .75 ->  number := number + .5  -> 34574.5
>      else
>    if dec >= .75      ->  number := number + 1   -> 34575
>  (in this case, number will be 34574.5 because .5 <= dec < .75)
>    divide by 1000     ->  34.5745

  Correction:  In the line marked with an "*" above, the .5 values should
  actually be .25
  And in my roundnum function, I also made another error.  I put .35 where
  the correct number should have been .25

 >     Sam Lane     <
 >  saml...@wpi.edu <

Re:Rounding off a real number to half thousands


Re:Rounding off a real number to half thousands


On 18 Feb 1996 13:01:19 GMT, allan...@pennet.net (Allan Bremer) wrote:

Quote
>example:  34.574423  to  34.5745
>           3.123164  to   3.1230
>           0.012345  to   0.0120

Unless I mess up the decimal point:

Function Round(V:Real):Real;
BEGIN
  Round := ((V * 1000)+5)/1000;
END;

BTW, it's a good idea to rewrite ROUND anyway.  There's an insidious
bug in it up to BP7.  (Don't yet know about Delphi.)

--
Al

Re:Rounding off a real number to half thousands


Quote
Sam <saml...@wpi.edu> wrote:
>On 18 Feb 1996, Allan Bremer wrote:

>> Hello,

>> I am trying to round off real numbers (or a string) to half
>> thousands.

>> example:  34.574423  to  34.5745
>>            3.123164  to   3.1230
>>            0.012345  to   0.0120

>> You see, there rounded off to the nearest half thousand.  How can I
>> do this using Pascal.

>If you multiply the number by 1000, you can separate the integer and decimal
>part, and use the decimal part to determine if the integer part is to be
>rounded to next number, to .5, or down to 0
>Ex. 34.574423
>   Multi. by 1000     ->  34574.423
>   Get decimal        ->  dec := .423
>   Get integer        ->  number := 34574
>   If 0 <= dec < .5   ->  number := number       -> 34574
>      else
>   If .5 <= dec < .75 ->  number := number + .5  -> 34574.5
>     else
>   if dec >= .75      ->  number := number + 1   -> 34575
> (in this case, number will be 34574.5 because .5 <= dec < .75)
>   divide by 1000     ->  34.5745

>function roundnum(number:real) : real;
>var
>  dec : real;
>begin
>   number := number * 1000;
>   dec := frac(number);
>   number := int(number);

>   if (dec >= 0.35) and (dec < 0.75) then number := number + 0.5
>      else if (dec >= 0.75) then number := number + 1;

>   roundnum := number / 1000;
>end;

> >     Sam Lane     <
> >  saml...@wpi.edu <

Try multiplying by 2000 instead. Use Round or Trunc (depending on what you want to happen)
 and then devide it by 2000 again...

Re:Rounding off a real number to half thousands


Quote
Tom Wheeley <t...@tsys.demon.co.uk> wrote:
>In article <4g77uv$...@crash.microserve.net>
>           allan...@pennet.net "Allan Bremer" writes:

>> Hello,

>> I am trying to round off real numbers (or a string) to half
>> thousands.

>> example:  34.574423  to  34.5745
>>            3.123164  to   3.1230
>>            0.012345  to   0.0120

>> You see, there rounded off to the nearest half thousand.  How can I
>> do this using Pascal.

>Multiply by 200, Round normally to nearest integer, divide by 200.

>(I think...)

>--
>* TQ 1.0 * The 'Just So Quotes'.
>I keep hearing that Jesus Christ is coming, but nobody knows his tour dates.
>            -- Michael Lucas

200, that's what I meant!

Re:Rounding off a real number to half thousands


Quote
In article <4gaugh$...@slip.net> akl...@slip.net writes:
>BTW, it's a good idea to rewrite ROUND anyway.  There's an insidious
>bug in it up to BP7.  (Don't yet know about Delphi.)

How about telling us what the bug is, with example code?

--
John Stockton : mailto:J...@dclf.npl.co.uk from off-site.  MIME.  WP.
 National Physical Laboratory, Teddington, Middlesex, TW11 0LW, UK
  Direct Telephone +44 181-943 6087, Nearby Fax +44 181-943 7138  
   Offshore news still takes up to a week to get here;  please
    E-mail me a copy of non-UK non-pascal non-TV followups !  
     Regret system puts unzoned (UK civil) time on messages.

Other Threads