Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help with a rounding problem

Posted on 2004-10-19
12
Medium Priority
?
229 Views
Last Modified: 2010-04-05
I need some code (formula) that can return an integer value based on these rules:

1. No rounding

 value       returned value
  0.9            0
  1.1            1
  1.5            1
   .5             0

2. < 1 rounding

 value        returned value
   .9              0
   1.1            1
   1.5            2
    .5            0

3. < 0.5 rounding
 
  value        returned value
    .3               0
    .5               1
   1.5              2
    1.2             1

Obviously the value can be any decimal number so I need a formula or three that can handle this kind of rounding.

0
Comment
Question by:rutledgj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 400 total points
ID: 12348607
Hi,

1. Use Trunc function

2. What is the logic here?

3. Use:

function RoundUp(X: Extended): Int64;
begin
  Result := Trunc(X) + Trunc(Frac(X) * 2);
end;

Regards, Geo
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12348668
1.
var
  I:      Integer;
  D:      Double;
begin
  D := 0.9;
  I := Round(Int(D));
  Edit1.Text := IntToStr(I);
end;

2.
var
  I:      Integer;
  D:      Double;
begin
  D := 0.9;
  I := Round(D);
  Edit1.Text := IntToStr(I);
end;
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 12348788
Also check out the floor and ceil functions in the maths unit
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:esoftbg
ID: 12350702
procedure TForm1.Button1Click(Sender: TObject);
var
  I:      Integer;
  DA:     array[0..3] of Double;
begin
  DA[0] := 0.9;
  DA[1] := 1.1;
  DA[2] := 1.5;
  DA[3] := 0.5;
  for I := 0 to 3 do
    ListBox1.Items.Add(FloatToStr(Floor(DA[I])));
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  I:      Integer;
  DA:     array[0..3] of Double;
begin
  DA[0] := 0.9;
  DA[1] := 1.1;
  DA[2] := 1.5;
  DA[3] := 0.5;
  for I := 0 to 3 do
  begin
      if (DA[I]<1) then
        ListBox2.Items.Add(FloatToStr(Floor(DA[I])))
      else
        ListBox2.Items.Add(FloatToStr(Round(DA[I])));
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  I:      Integer;
  DA:     array[0..3] of Double;
begin
  DA[0] := 0.3;
  DA[1] := 0.5;
  DA[2] := 1.5;
  DA[3] := 1.2;
  for I := 0 to 3 do
  begin
      if (DA[I]<0.5) then
        ListBox3.Items.Add(FloatToStr(Floor(DA[I])))
      else
        ListBox3.Items.Add(FloatToStr(Round(DA[I]+0.00000001)));
  end;
end;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12350734
Post
Button1, Button2, Button3,
ListBox1, ListBox2, ListBox3
on the Form1 to see the results from above code.
0
 
LVL 17

Expert Comment

by:geobul
ID: 12356985
I'm surprised :-(  

BTW in the accepted answer:
- The first one will behave unexpectedly with negative numbers, i.e. '-1.5' will become '-2' instead of '-1'.
- I can't say anythung about 2 because I haven't understood the requirement.
- IMHO the last one is wrong. That constant depends on the type of the real variable and its specific representation (OS, processor, etc.).
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12362112
Hi Geo, you are right !
I did not think abot the negative values at all ....
Your solutions about [1.] and [3.] are elegant !

So, how we could solve I am graded in mistake ?
I think the page editor could redirect points to you.
0
 
LVL 12

Assisted Solution

by:esoftbg
esoftbg earned 200 total points
ID: 12362192
I provide here a correct version of the [2.] which works fine with both positive and negative values:

procedure TForm1.Button2Click(Sender: TObject);
type
  TSignIs = (siMinus, siPlus);
var
  I:      Integer;
  J:      Integer;
  DA:     array[0..7] of Double;
  SignIs: TSignIs;
begin
  DA[0] := -0.5;
  DA[1] := -1.5;
  DA[2] := -1.1;
  DA[3] := -0.9;
  DA[4] := 0.9;
  DA[5] := 1.1;
  DA[6] := 1.5;
  DA[7] := 0.5;
  for I := 0 to 7 do
  begin
    if (DA[I]>=0) then
      SignIs := siPlus
    else
    begin
      SignIs := siMinus;
      DA[I] := ABS(DA[I]);
    end;
    if (DA[I]<1) then
      J := Floor(DA[I])
    else
      J := Round(DA[I]);
    if (SignIs=siMinus) then
      J := - J;
    ListBox2.Items.Add(IntToStr(J));
  end;
end;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12367070
kretzschmar,
please redirect the points of this question to geobul,
because his solutions are correct and elegant.
Emil.
0
 
LVL 17

Expert Comment

by:geobul
ID: 12367204
Hi Emo,

The questioner is the one who decides which answer fits his needs. It is most likely that he doesn't care about negative numbers at all. So, there is no problem with the grading. I just wanted to leave my thougths for eventual future readers of this question. Hope it's clear now.

BTW. Congratulations, Emo, for your outstanding work here at Ex-Ex. I don't feel alone anymore ;-)
0
 

Author Comment

by:rutledgj
ID: 12368367
You are right. Negative numbers were not a concern.

0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question