/    Sign up×
Community /Pin to ProfileBookmark

Round in 5 incrimens?

I am coding a volume slider for my WinAMP remote, and the volume goes up in 5% increments.

The problem is in if someone changes the volume on the client side to something that doesn’t end in a 5 or 0.

Basically, what I need to have, is a function that takes a number, and returns that number rounded to the nearest 5 or 0. I’m really bad at this kind of logic. Can anyone help?

Thanks!
Chris

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@kurbyApr 07.2008 — PSEUDO CODE:


[CODE]remainder = volume mod 5;
if remainder != 0
if remainder >= 3
volume += remainder - 5;
else
volume -= remainder;[/CODE]
Copy linkTweet thisAlerts:
@Logic_AliApr 07.2008 — I am coding a volume slider for my WinAMP remote, and the volume goes up in 5% increments.

The problem is in if someone changes the volume on the client side to something that doesn't end in a 5 or 0.

Basically, what I need to have, is a function that takes a number, and returns that number rounded to the nearest 5 or 0. I'm really bad at this kind of logic. Can anyone help?

Thanks!

Chris[/quote]
<script type='text/javascript'>

Number.prototype.nearest=function(x)
{
return Math.round(this/x) * x;
}

for(var i=0; i<50; i++)
document.write( i+' to the nearest 5 = ' + i.nearest(5)+'<br>');

</script>
Copy linkTweet thisAlerts:
@mrhooApr 08.2008 — var n=Math.round(n/5)*5

like Logic's, but a one shot statement rather than a new number method.

Beginners always have problems with number methods-

you can call:

var n=54;

alert(n.nearest(5))

[B]or[/B] alert((54).nearest(5))

but you [B]can't[/B] use alert(54.nearest(5))
Copy linkTweet thisAlerts:
@aximbigfanauthorApr 08.2008 — PSEUDO CODE:


[CODE]remainder = volume mod 5;
if remainder != 0
if remainder >= 3
volume += remainder - 5;
else
volume -= remainder;[/CODE]
[/QUOTE]


Can someone put this more understandably?

I have this:

function volumer()
{
var output = cur_volume mod 5;
if (output!=0)
{
if (output>=3)
output += cur_volume - 5;
else
output -= output;
}

alert(output);
}

But it doesn't work.

Thanks!

Chris
Copy linkTweet thisAlerts:
@aximbigfanauthorApr 08.2008 — var n=Math.round(n/5)*5

like Logic's, but a one shot statement rather than a new number method.

Beginners always have problems with number methods-

you can call:

var n=54;

alert(n.nearest(5))

[B]or[/B] alert((54).nearest(5))

but you [B]can't[/B] use alert(54.nearest(5))[/QUOTE]


Nevermind, this worked! Thanks Mrhoo!!!!

Chris
×

Success!

Help @aximbigfan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...