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:
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?
- double d_round(double in_d,int r2dec)
-
{ double d_whl=0,d_dec=0,d_last=0,d_rnd=0,d_rlst=0;
-
int x, to_dec =10;
-
for (x = 0; x<r2dec; x++)
-
{to_dec = to_dec*10;}
-
d_whl = floor(in_d);
-
d_dec = in_d - d_whl;
-
d_dec = floor(d_dec*to_dec);
-
d_last = floor(d_dec/10)*10;
-
d_rnd = d_dec-d_last;
-
if(d_rnd>=5)
-
d_rlst = d_whl + ((d_last+10)/to_dec);
-
else
-
d_rlst = d_whl + (d_last/to_dec);
-
return d_rlst;
-
}
note: respond to: http://bytes.com/topic/c/answers/217...nding-function
This one even works better:
Rounds whole numbers with neg numbers (left of dec)
Rounds dec with positive numbers (right of dec) - #include <math.h>
-
double round2(double number, int r2dec)
-
{
-
double d_rtn, i_div, i_mlt;
-
i_div = pow(10, r2dec+1);
-
i_mlt = pow(10, r2dec);
-
d_rtn = floor((number + (5/i_div))*(i_mlt))/(i_mlt);
-
return d_rtn;
-
}
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;}
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: - double y = round(x*100)/100;
However, inexact floating-point representation may get in your way.
round() doesn't exist in MS Visual C++ 6.0
Sign in to post your reply or Sign up for a free account.
Similar topics |
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:
|
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
|
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...
|
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.
|
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
| |
by: Joe |
last post by:
Is there a way to get 1000000 to display as 1,000k using double.ToString()?
-Joe
|
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):
|
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.
|
by: Sanchit |
last post by:
Library Function for converrting double to string in GCC.
-Thanks
Sanchit
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |