473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 6288
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

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 =...
0
by: yulyos | last post by:
Hi, Round Number Function You can download a small example: Watch and copy examples of Source Code in VB.NET yulyos4vbnet@yahoo.com http://www.geocities.com/yulyos4vbnet Have a nice day
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...
2
by: Marcos Jose Setim | last post by:
Hello, I need to round one number in language C, but I did not obtain. I already tried to use the: #include <math.h> ceil(value); But it's shown one error:
4
by: Fuzzydave | last post by:
I have been using a round command in a few places to round a value to zero decimal places using the following format, round('+value+', 0) but this consistantly returns the rounded result of...
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...
6
by: dkirkdrei | last post by:
I am looking for a way to round a number (which will be displayed as a dollar amount) to the nearest nickel. Using the php round function only allows you to control the number of decimal places in...
1
by: dhutton | last post by:
Hello - Question... What would be the best way to round a number off and change it to currency with the below statement? SELECT Cycle AS BillCycle, SUM(PrevBillAmt) - SUM(Payment) +...
19
by: muddasirmunir | last post by:
can any body tell me how to round numbers in in exactly two decimal places i had use a function round(text1.text,2) but whenever there is zere(0) at the end it does not show it for eg for round...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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 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.