LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2005, 06:19 PM   #1
Opeth
LQ Newbie
 
Registered: Jun 2004
Posts: 6

Rep: Reputation: 0
C++ Rounding and Truncation


Could anybody tell me which way would round up a float when turning it into an integer? I have a problem where I must use cin to take in 5 decimal numbers and then display them as the nearest integers. I'm not sure if just assigning the decimals into ints would truncate or round them. But if it truncates them what would be a way to round them up? Also, I'm restricted to the simplest things in c++ right now, not even loops or anyting, as we are only on chapter 2 in the class.
 
Old 09-17-2005, 06:41 PM   #2
glvgfz
LQ Newbie
 
Registered: Sep 2005
Location: Sydney, Australia
Distribution: Vector Linux
Posts: 26

Rep: Reputation: 15
In C, you have
Quote:
double round(double x);
float roundf(float x);
long double roundl(long double x);
defined in <math.h>

In C++, you would probably want to use equivalent functions in <cmath>.

btw, nice taste in music.
 
Old 09-17-2005, 06:44 PM   #3
glvgfz
LQ Newbie
 
Registered: Sep 2005
Location: Sydney, Australia
Distribution: Vector Linux
Posts: 26

Rep: Reputation: 15
Re: C++ Rounding and Truncation

If you want to round up or down, use ceil() and floor() functions.

look at round(3)
Code:
man 3 round
and look at where it says "SEE ALSO"
 
Old 09-17-2005, 06:51 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Or if you don't want to use any function at all, the following is a quick and dirty way to do it. (The casts are there just to get rid of the warnings.)

Code:
#include <iostream>
using namespace std;

int main()
{
    float f1 = 0.5;
    float f2 = 0.6;
    float f3=0.3;
    int i1 = static_cast<int>(f1 + 0.5);
    int i2 = static_cast<int>(f2 + 0.5);
    int i3 = static_cast<int>(f3 + 0.5);

    cout << i1 << endl
        << i2 << endl
        << i3 << endl;

    return 0;
}
 
Old 09-17-2005, 07:16 PM   #5
Opeth
LQ Newbie
 
Registered: Jun 2004
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks to all for your suggestions, but I probably should have been more clear in that last part of my post in that this was meant to be done without any functions. He says that the only functions we are learning this semester are cout and cin, which sucks, so I guess I'll take some time when I get a little more familiar to mess around with others. Thank you a lot for that deiussum, although I was hoping for less typing. I guess it's just that c++ chooses to truncate rather than round.

glvgfz, Opeth rock indeed.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
HEELLP ~~ Converting an integer minute into time, Rounding off a double ~~ HELLP Mistro116@yahoo.com Programming 5 10-04-2005 11:51 AM
truncation failed in endfile manojg Linux - Newbie 1 07-13-2005 11:59 AM
filename truncation Perl belmer2 Programming 1 04-24-2005 01:31 PM
Java Precission rounding. Tru_Messiah Programming 4 05-14-2004 11:23 PM
Truncation problem Big Al Debian 3 04-12-2004 08:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration