473,480 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

rounding

I am trying to write a rounding function. Rounding to 0.05. e.g.

I should get below results

6.125 --6.15
1.699 --1.7
1.1985 --1.20
0.5625 --0.60

Can someone have any sample for this....Plain C/C++ function will also do. I want to write more of a generic function so that it could be used from C/C++ programs so I will avoid using the C# advanced functions.

thanks

Jun 4 '07 #1
6 4695
You have to explain how the first and last cases are supposed to work. Rounding .5625 to two places should result in .57 and rounding 6.125 to two places should result in 6.13, using mathematical rounding. What rounding are you doing?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"abcd" <ab**@abcd.comwrote in message news:E8**********************************@microsof t.com...
I am trying to write a rounding function. Rounding to 0.05. e.g.

I should get below results

6.125 --6.15
1.699 --1.7
1.1985 --1.20
0.5625 --0.60

Can someone have any sample for this....Plain C/C++ function will also do. I want to write more of a generic function so that it could be used from C/C++ programs so I will avoid using the C# advanced functions.

thanks

Jun 4 '07 #2
On Mon, 04 Jun 2007 11:54:43 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
You have to explain how the first and last cases are supposed to
work. Rounding .5625 to two places should result in .57 and rounding
6.125 to two places should result in 6.13, using mathematical rounding..
What rounding are you doing?
It seems to me that he wants the rounding to round to multiples of 0.05.

To the OP, you can accomplish this in the specific case like this:

double RoundToFiveHundredths(double num)
{
return Math.Round(num * 2.0, 1) / 2.0;
}

Assuming you are always dealing with multiples that are themselves
divisible into 1.0, you can do it generally like this:

double RoundToMultiple(double num, double multiple)
{
double scale = 1.0 / multiple;

return Math.Round(num * scale) / scale;
}

Note that if you pick something that is theoretically divisible into 1.0,
but which is not representable in binary (eg 1/3, or 0.333...), you may
wind up wind rounding error. But this would be unavoidable, since even if
rounded perfectly, you would not be able to represent the result exactly
in binary form (the same thing holds true for decimal types, though with
different specific values of course).

Pete
Jun 4 '07 #3
That's just kind of odd....
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 04 Jun 2007 11:54:43 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
You have to explain how the first and last cases are supposed to
work. Rounding .5625 to two places should result in .57 and rounding
6.125 to two places should result in 6.13, using mathematical rounding.
What rounding are you doing?
It seems to me that he wants the rounding to round to multiples of 0.05.

To the OP, you can accomplish this in the specific case like this:

double RoundToFiveHundredths(double num)
{
return Math.Round(num * 2.0, 1) / 2.0;
}

Assuming you are always dealing with multiples that are themselves
divisible into 1.0, you can do it generally like this:

double RoundToMultiple(double num, double multiple)
{
double scale = 1.0 / multiple;

return Math.Round(num * scale) / scale;
}

Note that if you pick something that is theoretically divisible into 1.0,
but which is not representable in binary (eg 1/3, or 0.333...), you may
wind up wind rounding error. But this would be unavoidable, since even if
rounded perfectly, you would not be able to represent the result exactly
in binary form (the same thing holds true for decimal types, though with
different specific values of course).

Pete
Jun 4 '07 #4
On Mon, 04 Jun 2007 13:12:47 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
That's just kind of odd....
You've never heard of prices rounded to, for example, the nearest nickel?

Seems to me like a reasonable, even if uncommon, sort of thing to do.

Pete
Jun 4 '07 #5
Thanks ALL.
"abcd" <ab**@abcd.comwrote in message news:E8**********************************@microsof t.com...
I am trying to write a rounding function. Rounding to 0.05. e.g.

I should get below results

6.125 --6.15
1.699 --1.7
1.1985 --1.20
0.5625 --0.60

Can someone have any sample for this....Plain C/C++ function will also do. I want to write more of a generic function so that it could be used from C/C++ programs so I will avoid using the C# advanced functions.

thanks

Jun 4 '07 #6
BTW, simple formula (rounding up, assuming positive numbers) is:

((int)(x*20.0) - 1) / 20.0 + 1.0
"abcd" <ab**@abcd.comwrote in message
news:17**********************************@microsof t.com...
Thanks ALL.
"abcd" <ab**@abcd.comwrote in message
news:E8**********************************@microsof t.com...
I am trying to write a rounding function. Rounding to 0.05. e.g.

I should get below results

6.125 --6.15
1.699 --1.7
1.1985 --1.20
0.5625 --0.60

Can someone have any sample for this....Plain C/C++ function will also do.
I want to write more of a generic function so that it could be used from
C/C++ programs so I will avoid using the C# advanced functions.

thanks
Jun 5 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6713
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...
4
7812
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. ...
8
2062
by: Zorpiedoman | last post by:
Howcome: Dim D as decimal = .5D msgbox d.Round(D, 0) this returns "0" Now when I went to school .5 rounds UP to 1 not DOWN to zero?????!!! Documentation says this, but what the heck are...
2
2594
by: Jiri Nemec | last post by:
Hello all, I have got one table with rounding values, table contains prices and round types. id price_from price_to rounding 1 0 1500 0.1 2 1500 ...
11
6617
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
18
2192
by: jdrott1 | last post by:
i'm trying to round my currency string to end in 9. it's for a pricing application. this is the function i'm using to get the item in currency: FormatCurrency(BoxCost, , , , TriState.True) if...
5
7986
by: Spoon | last post by:
Hello everyone, I don't understand how the lrint() function works. long lrint(double x); The function returns the nearest long integer to x, consistent with the current rounding mode. It...
206
13075
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
4952
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
30
29418
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
6904
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
7034
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,...
1
6732
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
6886
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...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4472
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...
0
2990
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
1294
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 ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.