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 1755
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(valu e / 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(valu e / 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
representation on some platform might make it unsafe there (i.e. get the
wrong value due to the floating point value being an approximation) and
if so, is there a Better Way).
-- Russell
|
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 = form.ppi.value;
var hite = form.ht.value;
if (weight > 0.0) {
form.totwid.value = int_zero( weight * 2 )+(pgs/ppi)+0.5;
|
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 div which I'm using for body text. Floated
to the right of this are two image boxes and a broken out quotation from the
page (this box is wider than the images). The layout is working, except that
the text in the main section doesn't seem to wrap...
|
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 and will convert
between
date/time and double automatically. The double value Access (or VB)
creates is based on 1 day = 1.0 and the fractional part represents a
|
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 different.
Programming Microsoft Windows with Microsoft Visual Basic.NET
"The Round method with a single argument return the whole number nearest
to the argument. If the argument to Round is midway between two whole
numbers,
| |
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 "Configuration Properties" tree at the left, then select
"General" under "Custom Build Step"), what is the implicitly dependent file?
My situation is, there is a file I would like to copy after the build; but,
only if it has changed. I've added a "copy...
|
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 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)
|
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 returns me 2, which it is
not correct for my business rule.
|
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| | |