473,395 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Rounding form a float to an int in C++

I have to set up a program that has the user enter the miles they traveled, the hours it took and then, the output has to give them the mph and round it. How do I get started?
Sep 21 '09 #1
9 2889
Banfa
9,065 Expert Mod 8TB
In C or C++?

Look for a basic tutorial in the language you want to use. It should cover user input, output and simple arithmetic.
Sep 21 '09 #2
In C++, I know the basic set up but the rest like the math part of it and what characters to use throws me off.
Sep 21 '09 #3
donbock
2,426 Expert 2GB
First, let's clarify the requirements ...
  • How many decimal digits do you have to round to? (The obvious answer is zero, but I'm asking to make sure.)
  • There are several common rounding algorithms: round toward zero (truncation); round towards +/- infinity; round-up (ceil function), round-down (floor function), and round-to-nearest. Which one do you intend to use?

Hint: What happens when you assign a floating-point value to an integral variable? For example:
Expand|Select|Wrap|Line Numbers
  1. int a;
  2. double b = 4.75;
  3. a = b;
Sep 21 '09 #4
I have to round to 0 decimal digits, up to the nearest whole number.
Sep 21 '09 #5
hi

You can user floor and ceil funtion in c++.
If decimal part of your number is greater than or equal to 0.5 you can use ceil function and if decimal part is lesser than 0.5 you can use floor function.

To know the decimal part pass the double value to an integer variable and then subtract that integer value from double value. After that use if else statements to choose floor or ceil function.

You can also use modf function that will seperate integer part and fractional part from a double variable.

Vipin Sharma
Sep 22 '09 #6
donbock
2,426 Expert 2GB
@vipin sharma
Be careful to test your algorithm with both positive and negative inputs. It is pretty easy to come up with an algorithm that works on positive numbers but not on negative numbers.

Whether the quoted algorithm works for negative numbers depends on the precise meaning of if decimal part of your number is less than 0.5 for that case.
Sep 22 '09 #7
donbock
2,426 Expert 2GB
This forum has a link to the C FAQ index in the PLEASE READ FIRST thread.

Take a look at C FAQ #14.6.
Sep 22 '09 #8
hi,

Yes Donbock is right, this algorithm might not be useful in case of negative numbers. As long as we are dealing with speed and time algorithm works fine.

Vipin Sharma
Sep 23 '09 #9
whodgson
542 512MB
//25/09/09 15:03 you could tackle it along these lines.
Expand|Select|Wrap|Line Numbers
  1.    double y=<MPH>; 
  2.     if((y-int(y))<0.5)
  3.     cout<<"average miles/hour is: "<<floor(y)<<endl;
  4.     else cout<<"average miles/hour is: "<<ceil(y)<<endl;
  5.  
Sep 25 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Steven T. Hatton | last post by:
I'm surprised I haven't hit this situation till now, but I don't believe I've had to deal with it before. I have a function that sets the components of a point class (QPoint from Qt). It takes...
3
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
9
by: itportal | last post by:
Hello, This will probably be a simple question. How can I round float/doubles to a specific digit after the comma/dot? For example we have float i = 1.234567. I want to round it to 1.234....
12
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
2
by: whojustdenyme | last post by:
Hey guys, I'm new to C and programming..so I need some help. I need a way to round an INPUT number to an INPUT value so for ex: enter a decimal: 3.1459 enter the number to round: 3 Answer:...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
by: Gabriel Genellina | last post by:
En Wed, 30 Jul 2008 00:56:55 -0300, <jyoung79@kc.rr.comescribi�: If all you need is to *display* the value - use formatting expressions like %f, the locale variants, the Template class, or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.