Click to See Complete Forum and Search --> : Round to nearest 5


Coocoran
09-07-2003, 05:58 AM
I have a script that calculates prices upwards from a base price - it currently rounds to the nearest whole dollar _ I would like it to round to the nearest whole 5

eg. 1073.50 > 1075

Any help would be greatly appreciated :)

function doRound(x) {
return Math.round(x);
}

function updateGross() {
var upRate = 80.0000;
var grossValue = document.forms["new_product"].products_price_net.value;
var newValue = grossValue * ((25 / 100) + 1);
var nextValue = newValue * ((30 / 100) + 1);

if (upRate > 0) {

grossValue = nextValue * ((upRate / 100) + 1);
}

document.forms["new_product"].products_price.value = doRound(grossValue);
}

BestZest
09-07-2003, 06:37 AM
Easy, just change the function as so:

function doRound(x) {
x = x/5;
Math.round(x);
x = x*5;
return x;
}

Hope thats what you want.

BestZest

Charles
09-07-2003, 06:58 AM
Or more simply, more consistent with JavaScript and more useful:

<script type="text/javascript">
<!--
Math.roundBy = function (n, r) {return Math.round(n / r) * r}

alert (Math.roundBy(42.3, 5));
// -->
</script>

Less consistent with the Math.round() method but perhaps more consistent with the Object oriented nature of JavaScript, no less useful and just a little bit more simple:

<script type="text/javascript">
<!--
Number.prototype.roundBy = function (r) {return Math.round(this / r) *r}

alert ((42.3).roundBy(5));
// -->
</script>

Coocoran
09-07-2003, 08:29 AM
Well... thanks for trying guys.... the first returns a decimal - the number ended with .25 in my test - I am after a whole number ending with 5 or zero .... the others pop up a browser alert :confused:

Perhaps I didn't explain it well...

What I need is to replace the function doround - the rest of the script I need... it progressively calculates markups.... I just need to round the final grossValue to either 5 or zero... no decimal.

eg.

$26.50 = $25
$27.50 = $30

it currently calculates:

$26.50 = $27
$27.50 = $28

Coocoran
09-07-2003, 08:57 AM
Ok... take that back

Thankyou Charles - just didn't see it at first - this works!

Math.roundBy = function (n, r) {
return Math.round(n / r) * r
}

function updateGross() {
var upRate = 100.0000;
var grossValue = document.forms["new_product"].products_price_net.value;
var newValue = grossValue * ((25 / 100) + 1);
var nextValue = newValue * ((30 / 100) + 1);

if (upRate > 0) {

grossValue = nextValue * ((upRate / 100) + 1);
}

document.forms["new_product"].products_price.value = Math.roundBy(grossValue, 5);
}

Khalid Ali
09-07-2003, 09:00 AM
Replace your
doRound(x) function with the one below

function doRound(x){
while((x%5)!=0){
x= Math.ceil(x+1);
}
return x;
}

I am sure it will do what you are expecting..:D

Coocoran
09-07-2003, 10:43 AM
Originally posted by Khalid Ali


I am sure it will do what you are expecting..:D

It sure does.... thanks guys! :cool:

Khalid Ali
09-07-2003, 10:51 AM
:D
You are welcome..

Mr J
09-08-2003, 01:30 PM
function round_to(x){
x=Math.ceil(x)
while(x%y != 0){
x++
}
return x;
}

If you don't roundup before the while loop

1074.5 to the nearest 5 will give you 1080 instead of 1075

Khalid Ali
09-08-2003, 01:46 PM
Originally posted by Mr J

If you don't roundup before the while loop

1074.5 to the nearest 5 will give you 1080 instead of 1075

You are wrong. and please do make sure what you write on these forums, its to help people not to miss guide them.
On top of that you should post only if you have something to add to the answers that have already been posted or a better solution other wise please do not waiste the forum resources

Khalid Ali
09-08-2003, 02:43 PM
I have....I mostly post answers after testing..

Mr J
09-08-2003, 02:46 PM
Try it for yourself khalid

<script>
function doRound(x){
while((x%5)!=0){
x= Math.ceil(x+1);
}
return x;
}

function two(x){
x=Math.ceil(x)
while(x%5 != 0){
x++
}
return x;
}
</script>


<a href="#null" onclick="alert(doRound(1074.5))">Khalid Ali function</a>

<a href="#null" onclick="alert(two(1074.5))">MrJ function</a>

Khalid Ali
09-08-2003, 03:00 PM
Sorry I Know I am correct,if you have a useles problem,,you can check out the script at this link

http://68.145.35.86/skills/javascripts/RoundToNextMod_X_value.html

Mr J
09-08-2003, 03:13 PM
There is a difference depending on wether the number is passed as an integer or a string

x="1074.5"

or

x=1074.5

Khalid Ali
09-08-2003, 03:15 PM
which means???

Mr J
09-08-2003, 03:27 PM
Which means your function give 2 completely different answers depending on the form of the number


<SCRIPT>
<!--
function doRound1(){
x=1074.5
while((x%5)!=0){
x= Math.ceil(x+1);
}
alert(x)
}

function doRound2(){
x="1074.5"
while((x%5)!=0){
x= Math.ceil(x+1);
}
alert(x)
}
//-->
</SCRIPT>

<a href="#null" onclick="doRound1()">function 1</a>
<a href="#null" onclick="doRound2()">function 2</a>

Khalid Ali
09-08-2003, 03:32 PM
Interesting,,did not see that JavaScript would make that much difference in parsing a string to number..thereticaly there should not be any difference because javaScript engine converts string to appropriate number format automatically..
Well now the original poster knows have the choice to devise his/her function properly...

shaking me head....interseting....;)

Mr J
09-08-2003, 04:54 PM
Thank you but I would appreciate you deleting the post where you put ...........


You are wrong. and please do make sure what you write on these forums, its to help people not to miss guide them.

as I believe I have made a valid point.