?
Solved

Rounding Function

Posted on 1998-08-18
4
Medium Priority
?
226 Views
Last Modified: 2010-04-01
Is there a function that rounds float variables into
integers?  For example, flt = 2.83; round ( flt ); // flt will now be 3.  If there isn't, do you know how to make one?
0
Comment
Question by:strider031598
[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
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
Motaz earned 200 total points
ID: 1170619
//Hello Strider, you can write this code, it is very simple !!
// You can easily add 0.5 to the float number and assign it to
// An integer variable

#include <stdio.h>

void main(){
  float f=2.83;
  int i;

  i=f + 0.5;    // Rounding function, just add 0.5 to the float number
              // to be rounded

  printf("\n%d",i);
}
0
 
LVL 7

Expert Comment

by:Motaz
ID: 1170620
//Sorry, last function would'nt work with negative numbers.
//This is the full version of round function. Just copy and //paste.. and it sould work.

#include <stdio.h>

int round(float f){
  if (f>0) return int(f+0.5);    // Posotive number
  else return int(f-0.5);        // Negative number
}

void main(){
  float f;
  int i;

  f=2.83;
  i=round(f);
  printf("\n%d",i);

  f=-2.83;
  i=round(f);
  printf("\n%d",i);
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 1170621
flt=floor(flt+0.5);  

printf("%.0f",2.83);  //will print the rounded value (and should properly round to even for 2.5)
0
 

Author Comment

by:strider031598
ID: 1170622
Thanks, Motaz!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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