471,090 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,090 software developers and data experts.

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 4533
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Norvin Laudon | last post: by
8 posts views Thread by Zorpiedoman | last post: by
2 posts views Thread by Jiri Nemec | last post: by
18 posts views Thread by jdrott1 | last post: by
5 posts views Thread by Spoon | last post: by
206 posts views Thread by md | last post: by
20 posts views Thread by jacob navia | last post: by
30 posts views Thread by bdsatish | last post: by

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.