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! :) 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! :)
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! :)
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! :)
>
>
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.
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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 !!!!
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| | |