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 5956 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(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;}
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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: 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...
|
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,...
|
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...
|
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 ...
| |
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...
| |