473,466 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to round number to custom step (0.25, 20, 100...)

Hi all,

I'd need a function that would rounds values like :
d = roundTo(64.2, 0.25) ' returns 64.25
d = roundTo(64.2, 10) ' returns 64
d = roundTo(64.2, 100) ' returns 100

well, that rounds a value to the given step...with rounding up if it is
it is right in the middle (e.g. 1.25 to 1.5 with a step of 0.25)

can anyone help with this one?

Thanks! :)

Oct 24 '06 #1
5 1744
Try this (in C#). It has not been tested. It might not be the best but I
think it will work.
public double roundTo(double numToRound, double step)
{

if (step == 0)
return numToRound;

//Calc the floor value of numToRound
double floor = ((long) (numToRound / step)) * step;

//round up if more than half way of step
double round = floor;
double remainder = numToRound - floor;
if (remainder step / 2)
round += step;

return round;

}
"ibiza" wrote:
Hi all,

I'd need a function that would rounds values like :
d = roundTo(64.2, 0.25) ' returns 64.25
d = roundTo(64.2, 10) ' returns 64
d = roundTo(64.2, 100) ' returns 100

well, that rounds a value to the given step...with rounding up if it is
it is right in the middle (e.g. 1.25 to 1.5 with a step of 0.25)

can anyone help with this one?

Thanks! :)

Oct 24 '06 #2
I found a bug. The comparision in the if statement should be >= instead of >
if (remainder >= step / 2)
round += step;

"vincent" wrote:
Try this (in C#). It has not been tested. It might not be the best but I
think it will work.
public double roundTo(double numToRound, double step)
{

if (step == 0)
return numToRound;

//Calc the floor value of numToRound
double floor = ((long) (numToRound / step)) * step;

//round up if more than half way of step
double round = floor;
double remainder = numToRound - floor;
if (remainder step / 2)
round += step;

return round;

}
"ibiza" wrote:
Hi all,

I'd need a function that would rounds values like :
d = roundTo(64.2, 0.25) ' returns 64.25
d = roundTo(64.2, 10) ' returns 64
d = roundTo(64.2, 100) ' returns 100

well, that rounds a value to the given step...with rounding up if it is
it is right in the middle (e.g. 1.25 to 1.5 with a step of 0.25)

can anyone help with this one?

Thanks! :)
Oct 24 '06 #3
well, thank you very much! very appreciated ^_^
works fine!

vincent wrote:
I found a bug. The comparision in the if statement should be >= instead of >
if (remainder >= step / 2)
round += step;

"vincent" wrote:
Try this (in C#). It has not been tested. It might not be the best but I
think it will work.
public double roundTo(double numToRound, double step)
{

if (step == 0)
return numToRound;

//Calc the floor value of numToRound
double floor = ((long) (numToRound / step)) * step;

//round up if more than half way of step
double round = floor;
double remainder = numToRound - floor;
if (remainder step / 2)
round += step;

return round;

}
"ibiza" wrote:
Hi all,
>
I'd need a function that would rounds values like :
d = roundTo(64.2, 0.25) ' returns 64.25
d = roundTo(64.2, 10) ' returns 64
d = roundTo(64.2, 100) ' returns 100
>
well, that rounds a value to the given step...with rounding up if it is
it is right in the middle (e.g. 1.25 to 1.5 with a step of 0.25)
>
can anyone help with this one?
>
Thanks! :)
>
>
Oct 24 '06 #4
ibiza wrote:
Hi all,

I'd need a function that would rounds values like :
d = roundTo(64.2, 0.25) ' returns 64.25
d = roundTo(64.2, 10) ' returns 64
d = roundTo(64.2, 100) ' returns 100

well, that rounds a value to the given step...with rounding up if it is
it is right in the middle (e.g. 1.25 to 1.5 with a step of 0.25)

can anyone help with this one?

Thanks! :)
Simply:

Shared Function RoundTo(value As Double, step As Double) As Double
Return Math.Round(value / step) * step
End Function

This will of course return the value 60 in the second case you listed,
not 64. I believe that's what you really intended.
Oct 24 '06 #5
wow, even simpler :)

and you got it right, the second case should have returned 60, my
mistake ;)

Göran Andersson wrote:
ibiza wrote:
Hi all,

I'd need a function that would rounds values like :
d = roundTo(64.2, 0.25) ' returns 64.25
d = roundTo(64.2, 10) ' returns 64
d = roundTo(64.2, 100) ' returns 100

well, that rounds a value to the given step...with rounding up if it is
it is right in the middle (e.g. 1.25 to 1.5 with a step of 0.25)

can anyone help with this one?

Thanks! :)

Simply:

Shared Function RoundTo(value As Double, step As Double) As Double
Return Math.Round(value / step) * step
End Function

This will of course return the value 60 in the second case you listed,
not 64. I believe that's what you really intended.
Oct 24 '06 #6

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

Similar topics

11
by: Russell E. Owen | last post by:
I realize this probably a stupid question, but...is it safe to round to the nearest integer by using int(round(val))? I suspect it is fine, but wanted to be sure that weird floating point...
8
by: Schklerg | last post by:
I have this script performing a calculation on a page on my site: <script language="JavaScript"> function compute_weight(form) { var weight = form.wt.value; var pgs = form.pgs.value; var ppi =...
4
by: sweep | last post by:
Hi there, I have a problem with some CSS I'm writing and I was looking for a little help. I have a nav bar at the top and left side of my page in CSS which work fine. The remaining space has a...
6
by: Penguin | last post by:
At some long ago time Steve Jorgensen answered thus: Subject: Re: How can I round a time? Newsgroups: comp.databases.ms-access Date: 1998/12/11 Access represents a date internally as a double...
9
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something...
2
by: Peter Ritchie | last post by:
In VS/VC 2003: Simple question: for the Custom Build Step for the project (right click the project in the Solution Explorer, select "Properties", select the "Custom Build Step" in the...
5
by: ibiza | last post by:
Hi all, I'd need a function that would rounds values like : d = roundTo(64.2, 0.25) ' returns 64.25 d = roundTo(64.2, 10) ' returns 64 d = roundTo(64.2, 100) ' returns 100 well, that rounds...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
13
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, I need to round a decimal value with a particular rule. For example: decimal a = 1.49m -1m decimal a = 1.5m -1m decimal a = 1.51m -2m I've tried Math.Round but with the value 1.5 it...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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,...
0
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...
0
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,...
0
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
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.