Solved

Round Function

Posted on 2001-08-11
13
461 Views
Last Modified: 2008-03-03
I am currently programming in a language called Visual Dataflex.    It can get the job done, but it lacks many of the creature comforts of C++ and VB.

There is on round decent round function and there is no Array type.  there is an Array class, but the syntax for using it and most other VDF stuff is very long and clunky.

Hence my problem.  I want to create my own Round function.  Back in C++, I made one with a CHAR and it was pretty easy.  This function will only need to round things after the decimal place.  Everything I've tried so far is very long and I was wondering if anyone can suggest an algorithm for rounding after the decimal point, assuming that I can't use an array.

Essentially, I want the function to take two arguments, the number and the number to the right of the decimal place (Just like in other languages).

I know this is painfully easy, but I'm having a block.

Thanks,

Bill

0
Comment
Question by:wgrmba
13 Comments
 
LVL 6

Expert Comment

by:Triskelion
ID: 6376242
>>This function will only need to round things after the decimal place.  

Can you simply add the logic of:
   If the number after the decimal is greater than or equal to 50 then add one to the number before the decimal and return it?
0
 
LVL 1

Expert Comment

by:MatthewL
ID: 6376736
Hi wgrmba,

You can add .5 to the number and then cast to an integer.

To handle rounding to the 1 decimal place, multiply by 10 add .5, cast, divide by 10

To handle rounding to the nth decimal place, muliply by 10^n, add .5, cast, divide by 10^n

Matt
0
 
LVL 2

Expert Comment

by:TrueDrake
ID: 6377963
Hi,

z = 1.56
x = Round(z, 1)
MsgBox x

Enjoy
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 16

Expert Comment

by:t0t0
ID: 6378281

integer = ABS(float)

IF (float - integer >= 0.5)
  integer = integer + 1
END-IF

or in C


.
.
i = abs(f);

if (f - i >= 0.5)
  i++;

.
.


0
 
LVL 11

Expert Comment

by:griessh
ID: 6382183
0
 
LVL 11

Expert Comment

by:griessh
ID: 6382188
0
 
LVL 11

Expert Comment

by:griessh
ID: 6382199
0
 
LVL 11

Expert Comment

by:griessh
ID: 6382201
0
 
LVL 11

Expert Comment

by:griessh
ID: 6382205
0
 
LVL 11

Expert Comment

by:griessh
ID: 6382724
I can't post anymore ... *&@*&$*&)(&(*^%!!!!
0
 
LVL 11

Accepted Solution

by:
griessh earned 100 total points
ID: 6382726
AHHHH! It works again. Sorry, but I never got anything through to EE. So here you go:
Since I don't know the syntax of your language, here a very general function for rounding to a specific decimal. It is really just to give you the idea...

float round (float f, int dec)
{
  int k =1;
  for (i=0; i<dec; i++) { // Multiply by dec 10s
     f=f*10;
     k=k*10;
  }
  // Rounding is +.5 then take int
  f = f+0.5;
  int res = int (f);

  // Make it a float again
  float ret = ((float) res) / k;

  return (ret);
}

======
Werner
0
 
LVL 84

Expert Comment

by:ozo
ID: 6385806
That doesn't work for negative numbers.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6385860
ozo

Thanks for the comment. As I said: "It is really just to give you the idea...".

There is NO checking of any kind in this function! But I guess that was not asked for anyhow ...

======
Werner
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
word0 challenge 4 68
Arduino EDI - Programming language 3 95
creating threads in delphi 1 105
backup program with robocopy 6 19
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

810 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