473,513 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Rounding Function for ansi C (using double)

8 New Member
Question: http://bytes.com/topic/c/answers/217...nding-function
Is there a std lib rounding function that will round a real to a
given number of decimal places? Something like:

double round_it(double number, int decimal_digits)

pass 34.5678, 3 to it and it returns 34.568
Note: posting solution to archived question:
Jul 14 '11 #1
6 5956
donbock
2,426 Recognized Expert Top Contributor
You didn't ask a question. Does this function do what you want it to do?
Jul 15 '11 #2
Darren Schehl
8 New Member
Expand|Select|Wrap|Line Numbers
  1. double d_round(double in_d,int r2dec)
  2. { double d_whl=0,d_dec=0,d_last=0,d_rnd=0,d_rlst=0;
  3.   int x, to_dec =10;
  4. for (x = 0; x<r2dec; x++)
  5.     {to_dec = to_dec*10;}
  6. d_whl = floor(in_d);
  7. d_dec = in_d - d_whl;
  8. d_dec = floor(d_dec*to_dec);
  9. d_last = floor(d_dec/10)*10;
  10. d_rnd = d_dec-d_last;
  11. if(d_rnd>=5)
  12.   d_rlst = d_whl + ((d_last+10)/to_dec);
  13. else
  14.   d_rlst = d_whl + (d_last/to_dec);
  15. return d_rlst;
  16. }
note: respond to: http://bytes.com/topic/c/answers/217...nding-function
Jul 15 '11 #3
Darren Schehl
8 New Member
This one even works better:
Rounds whole numbers with neg numbers (left of dec)
Rounds dec with positive numbers (right of dec)

Expand|Select|Wrap|Line Numbers
  1. #include <math.h>
  2. double round2(double number, int r2dec)
  3. {
  4.  double d_rtn, i_div, i_mlt;
  5.     i_div = pow(10, r2dec+1);
  6.     i_mlt = pow(10, r2dec);
  7.     d_rtn = floor((number + (5/i_div))*(i_mlt))/(i_mlt);
  8.  return d_rtn;
  9. }
Jul 15 '11 #4
Darren Schehl
8 New Member
This one handles negative numbers also.
#include <math.h>
double round_dbl(double number, int r2dec)
{double d_rtn, i_div, i_mlt;
int half = 5;
if(number<0) half = -5;
i_div = half/pow(10,r2dec+1);
i_mlt = pow(10, r2dec);
d_rtn = int((number + i_div)*(i_mlt))/(i_mlt);
return d_rtn;}
Jul 19 '11 #5
donbock
2,426 Recognized Expert Top Contributor
The following standard library functions are available in C99 to help you with rounding:
ceil() Rounds towards +infinity.
floor() Rounds towards -infinity.
round() Rounds to nearest integer.
trunc() Rounds towards zero.
nearbyint() Rounds as per the current rounding direction.
rint() same as nearbyint(), but handles errors differently
fesetround() Set the current rounding direction.
All of these functions round to the nearest integer. It is a simple matter to round to any decimal digit:
Expand|Select|Wrap|Line Numbers
  1. double y = round(x*100)/100;
However, inexact floating-point representation may get in your way.
Jul 20 '11 #6
Darren Schehl
8 New Member
round() doesn't exist in MS Visual C++ 6.0
Jul 20 '11 #7

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

Similar topics

2
1971
by: actuary77 | last post by:
I am trying to write simple recursive function to build a list: def rec(n,alist=): _nl=alist print n,_nl if n == 0: print n,_nl return _nl else:
6
9002
by: Curley Q. | last post by:
Is there a std lib rounding function that will round a real to a given number of decimal places? Something like: double round_it(double number, int decimal_digits) pass 34.5678, 3 to it and it...
0
1138
by: Nikki | last post by:
Hi, I m developing a windows c#.NET application in which text scrolls from right to left (STOCK TICKER). While scrolling the text keeps changing. (TICKER VALUES KEEP UPDATING) Now the problem that...
2
1653
by: Henry | last post by:
Hi guys, I want to write some global functions which can be called from different asp.net page. In Visual Basic, there is a global module which allow me to do that. In Visual Basic .net, I...
7
3626
by: Li Pang | last post by:
Hi, I'd like to know how to call a sub/function bu using a string containing its name, as an example as follow: Sub Main() Dim name As String = "TestMe()" ' need help here End Sub
1
1797
by: Joe | last post by:
Is there a way to get 1000000 to display as 1,000k using double.ToString()? -Joe
1
2761
by: HYRY | last post by:
I want to add some simple search function for my homepage. It need to search through all the html files of my homepage (about 300 pages), and highlight the search words. I made some test with...
11
2847
by: dolphin | last post by:
Hi All! I have a question that how to call a function just using a string. For example There is a .cpp file named a.cpp.There are some functions::fun1() fun2() fun3(). I have another fucntion...
6
1624
by: Sanchit | last post by:
Library Function for converrting double to string in GCC. -Thanks Sanchit
0
7260
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
7162
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
7384
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
7539
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...
1
7101
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
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.