简体   繁体   English

使用上限功能的Javascript,其值小于1

[英]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 因此,我有一个项目列表,如果要显示(例如,每页10个项目),我想检查一下需要多少页

ie

220 should give me 22 pages 220应该给我22页

134 should give me 14 pages 134应该给我14页

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. 我遇到的问题是,如果我有7个项目,我希望它能给我1页。 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? 大于1,如何解决此问题?

I think your problem lies elsewhere. 我认为您的问题出在其他地方。 Consider the following Math.ceil operations: 考虑以下Math.ceil操作:

> 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. 看起来Math.ceil提供了预期的1 ,除非您的7值格式错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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