简体   繁体   中英

Javascript using the ceiling function for value less than 1

So, I have a list of items and I want to check how many pages I would need if I want to show , say, 10 items per page

ie

220 should give me 22 pages

134 should give me 14 pages

I am using the ceiling function to get the number of pages

var pages = parseInt(items)/10;
alert("pages " +pages);
alert(Math.ceil(pages));

The problem which I am having is if I have 7 items, I am expecting it to give me 1 page. However, I don't get any output. I have noticed that I only get an output if the value for pages from

var pages = parseInt(items)/10;

is greater than 1 , How do I fix this problem?

I think your problem lies elsewhere. Consider the following Math.ceil operations:

> Math.ceil(220/10)
22
> Math.ceil(134/10)
14
> Math.ceil(7/10)
1

Then look at the operation that you might have handled -- a string version of a number:

> Math.ceil(parseInt("7")/10);
1
> Math.ceil(parseInt(" 7")/10);
1
> Math.ceil(parseInt(" 7 ")/10);
1

It would appear that Math.ceil is providing 1 as expected, unless your value of 7 is malformed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM