473,671 Members | 2,288 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 5961
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(doubl e 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
1978
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
9011
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 returns 34.568
0
1150
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 I m facing is that though using double buffering, Sometimes it appears that images is not been cleared properly and thus the updated text overlapps with the previous value. What can be the possible solution for this problem. Each time I am...
2
1660
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 have to create a class file to host these functions. And every time when I need to call these functions, I have to create a object.
7
3633
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
1811
by: Joe | last post by:
Is there a way to get 1000000 to display as 1,000k using double.ToString()? -Joe
1
2763
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 HTMLParser, it works but slow. So, my question is how can I improve its speed? from HTMLParser import HTMLParser class HightLightParser(HTMLParser):
11
2862
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 void funcall( char *pch). if I pass a argument char* p1="fun1" .How do I call the function fun1() using that string "fun1"that I pass.
6
1637
by: Sanchit | last post by:
Library Function for converrting double to string in GCC. -Thanks Sanchit
0
8471
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8907
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8817
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8593
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8663
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4215
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2804
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 we have to send another system

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.