/    Sign up×
Community /Pin to ProfileBookmark

Rounding Number

Anyone know a function that rounds up a number to the nearest ten or hundred or thousand etc.

Basically I would like to create a function that would round these numbers to these:

25-30
51-60 etc.

then if the number is in the hundreds then

101-200
240-300 etc.

then if the number is in the thousands then

1001 to 2000 etc.

This may not even be possible but would quite like to know. If someone could tell me how to round up to nearest thousand then I should be able to work out the rest.

Thanks in Advance,

Ben

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@badbenFeb 25.2010 — [code=php]<?php
function roundTo($number, $to){
return round($number/$to, 0)* $to;
}

echo roundTo(87.23, 20); //80
echo roundTo(-87.23, 20); //-80
echo roundTo(87.23, .25); //87.25
echo roundTo(.23, .25); //.25
?>[/code]
Copy linkTweet thisAlerts:
@Benji6996authorFeb 25.2010 — thanks for trying but not what I was looking for, I don't need the function anymore as I have found an alternate solution.

Thanks
Copy linkTweet thisAlerts:
@MindzaiFeb 25.2010 — 1:30am is not my favourite time for maths, but this should do the job:

[code=php]function ceil_scaled($value) {
$precision = pow(10, strlen($value) - 1);
return ceil(intval($value) / $precision) * $precision;
}

echo ceil_scaled(25); // 30
echo ceil_scaled(51); // 60
echo ceil_scaled(101); // 200
echo ceil_scaled(240); // 300
echo ceil_scaled(1001); // 2000[/code]
Copy linkTweet thisAlerts:
@Benji6996authorFeb 25.2010 — You the man Mindzai!! Thanks
×

Success!

Help @Benji6996 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.7,
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,
)...