473,789 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ 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 4708
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.co m
"abcd" <ab**@abcd.comw rote in message news:E8******** *************** ***********@mic rosoft.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.c omwrote:
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 RoundToFiveHund redths(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.co m

"Peter Duniho" <Np*********@nn owslpianmk.comw rote 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.c omwrote:
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 RoundToFiveHund redths(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.c omwrote:
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.comw rote in message news:E8******** *************** ***********@mic rosoft.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.comw rote in message
news:17******** *************** ***********@mic rosoft.com...
Thanks ALL.
"abcd" <ab**@abcd.comw rote in message
news:E8******** *************** ***********@mic rosoft.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
6742
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 integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is
4
7833
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. I need it to contain 180.26. Any ideas?
8
2087
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 they thinking??? I just don't
2
2643
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 5000 1 3 5000 15000 10 4 15000 0 100
11
6658
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 seems to be done like this 3.435 = 3.44 3.445 = 3.44 Dim decNbr1 As Decimal
18
2232
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 an item is 41.87 i want the application to bring it up to 41.89 if an item is 41.84 i want the application to round down to 41.79 does anyone know how i could do this?
5
8016
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 raises an invalid floating-point exception if the magnitude of the rounded value is too large to represent. And it raises an inexact floating-point exception if the return value does not
206
13329
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) It then updates the value with numberOfPrecisions after the decimal
20
5016
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
29690
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 expected my_round(2.5) = 2 # Not 3, which is an odd num I'm interested in rounding numbers of the form "x.5" depending upon whether x is odd or even. Any idea about how to implement it ?
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10195
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...
0
9016
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6765
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
5415
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...
1
4090
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
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.