Solved

Rounding Function

Posted on 1998-08-18
4
221 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 50 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 190
Why isn't object file created? 6 104
Installshield for Embarcadero EX 10.1 Berlin 4 86
what type of pointer is this pointer? 3 29
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

738 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