/    Sign up×
Community /Pin to ProfileBookmark

Custom Ceiling…

I’ve just started diving into Javascript and noticed that the Math.ceil() function only takes 1 parameter and just rounds up to the next whole number. So I was wondering if anyone has written (or has any ideas on writing) a ceiling function with 2 parameters, like the M$ Excel one. For example:
ceil( .68, 1) = 1
ceil( .68, .25) = .75

I’ve written one in C++ for PalmOS, because it too only has the 1 parameter function. But porting it to JS would be a complete pain, especally because I’m just getting into JS. So if anyone has done this before and would like to help out, it would be greatly appreciated.

Z

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@toicontienFeb 17.2006 — I take it this function should return the greater of the two ceilings? If that's the case, create a wrapper function for the math.ceil() function.

<i>
</i>function getLargestCeiling(num1, num2) {
var ceiling1 = Math.ceil(num1);
var ceiling2 = Math.ceil(num2);

if (ceiling1 &gt; ceiling2) {
return ceiling1;
} else {
return ceiling2;
}
}

Then elsewhere in your javascript, you can call it thusly (is that a word?):

<i>
</i>biggestCeiling = getLargestCeiling(6.25, .04);

If you need an even more flexible function that takes an unknown number of numbers, try this:
<i>
</i>var ceiling = getLargestCeiling(Array(2.5, 3.009, 22.6, 3.00007));

function getLargestCeiling(numbers) {
var returnCeiling = 0;
var tempCeiling = 0;

for (var i = 0; i &lt; numbers.length; i++) {
tempCeiling = Math.ceil(numbers[i]);
if (tempCeiling &gt; returnCeiling) {
returnCeiling = tempCeiling;
}
}

return returnCeiling;
}

You can send as many numbers as you want inside that Array() function call. Just separate each number with a comma. You might also be able to call the function like this:

<i>
</i>var ceiling = getLargestCeiling([2.5, 3.009, 22.6, 3.00007]);

Or if you already have a pre-existing array containing numbers:

<i>
</i>var numbers = new Array();
var ceiling = 0;

numbers[0] = 2.5;
numbers[1] = 3.009;
numbers[2] = 22.6;
numbers[3] = 3.00007;

ceiling = getLargestCeiling(numbers);
Copy linkTweet thisAlerts:
@KravvitzFeb 17.2006 — Hmm... no. It looks like he wants to round the first number to the closest multiple of the second number.

Why would you want to do that?
Copy linkTweet thisAlerts:
@ZartolauthorFeb 18.2006 — Hmm... no. It looks like he wants to round the first number to the closest multiple of the second number.

Why would you want to do that?[/QUOTE]


Yes, that's it exactly. There are places where I need to round up/down to the nearest 1/8th or 1/16th.
Copy linkTweet thisAlerts:
@BrainEaterFeb 18.2006 — You want it when you are working with or selling material the come in discrete quantities that aren't necessarily whole numbers.Math.ceil2=function(a,b){var b=b||1;try{var c=Math.pow(10,b.toString().split('.')[1].length)}catch(e){var c=1}return Math.round(Math.ceil(a/b)*b*c)/c;}

alert(Math.ceil2(2.0505,.0625)+'n'+
Math.ceil2(2.0505,.125)+'n'+
Math.ceil2(2.0505,.25)+'n'+
Math.ceil2(2.0505,.5)+'n'+
Math.ceil2(2.0505))

as well asMath.floor2=function(a,b){var b=b||1;try{var c=Math.pow(10,b.toString().split('.')[1].length)}catch(e){var c=1}return Math.round(Math.floor(a/b)*b*c)/c;}

Math.round2=function(a,b){var b=b||1;try{var c=Math.pow(10,b.toString().split('.')[1].length)}catch(e){var c=1}return Math.round(Math.round(a/b)*b*c)/c;}
×

Success!

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