473,386 Members | 1,644 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,386 software developers and data experts.

about rounding off numbers

Hi,

I have a small question.

If i need to round off a number to int, i do it like
int j = (int)(4.8);
thus "j" will have value 4;

but my requirement is to see that if the value is 4.8 then "j" should take value as 5 and if the value is 4.3 then "j" should take value as 4.

How can I do this in C++.

Thanks!!
Jul 6 '07 #1
5 2215
weaknessforcats
9,208 Expert Mod 8TB
Use a stringstream:
Expand|Select|Wrap|Line Numbers
  1. int Rounder(double d)
  2. {
  3.     stringstream str;
  4.     str << d;
  5.     int result;
  6.     char decimalpoint;
  7.     int decimal;
  8.     str >> result >> decimalpoint >> decimal;
  9.     if (decimal > 5)
  10.     {
  11.         ++result;
  12.     }
  13.     return result;
  14. }
  15. int main()
  16. {
  17.     double d1 = 4.8;
  18.     cout << Rounder(d1) << endl;
  19.     double d2 = 4.3;
  20.     cout << Rounder(d2) << endl;
  21. }
  22.  
The notion of doing a cast is not rounding. That is covering up a compiler warning about truncation an possible loss of data.

The Rounder() function just reads the double into a stringstream and then extracts it as an int, char , int. Now you can make a test on the decmal portion and decide whether to increment the result or not.

Remember, when you cast in C++ you a) are calling a relic C function with a void* argument or b) your C++ design is faulty.
Jul 6 '07 #2
Thanks for the clear explanation!!
Jul 6 '07 #3
Hi,

the above is applicable only when i have a value between 4 and 5.
But in my program I am computing certain values and those values change each time in the loop. then in such case how can i round off as above??

do we have any in built functions for that??

Thanks!!
Jul 6 '07 #4
r035198x
13,262 8TB
Hi,

the above is applicable only when i have a value between 4 and 5.
...
No it is not limited to that range only.
Jul 6 '07 #5
Another option is to add 0.5 onto the number and then floor it, and then cast the result to an int.

if num is a double variable, then this would do it:

Expand|Select|Wrap|Line Numbers
  1. int j = static_cast<int>(floor(num + 0.5));
I believe you'll need to include <cmath> to use the floor function.

this works because the floor function always rounds down, and any number that with a decimal of .5 or higher is raised to the next integer before being rounded. so that with 4.3 is increased to 4.8 and then floored giving you 4; while 4.8 would be raised to 5.3 which is floored to 5.

This is assuming that you want a decimal value of .5 to round up, if you want .5 to round down, then you can do this:

Expand|Select|Wrap|Line Numbers
  1. int j = static_cast<int>(ceil(num - 0.5));
again needing to include <cmath> would be required.
Jul 6 '07 #6

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

Similar topics

2
by: Christopher T King | last post by:
Speakign of rounding errors, why is round() defined to round away from zero? I've always been quite fond of the mathematical definition of rounding of floor(n+.5). As a contrived example of why the...
13
by: dhildebrandt | last post by:
I have a query that uses the Round function to change decimals into whole numbers. For 4 years straight the thing has always rounded the numbers in exactly the same way so that whenever I update...
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...
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
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.