Parts per 100 return an integer
I'm开发者_运维知识库 looking for an algorithm to return the number of parts per 100.
For example, if the part is between 1 to 100 then it return 1. If the part is between 101 to 200 then it should return 2. Final example, if the part is 357 it should return 4.
A simple division will not work and tried modulo but cannot get it to give me the right answer. Can someone help me with this?
You can simply divide by 100 and ceil the value.
What language are you using?
PHP example:
$part = ceil($number/100);
Language is important here but usually you can either use a ceiling function, or cast the numbers as integers and add 1 to it like I have below for C++
int parts_per_hundred(int value) {
// value / 100 will give you an integer.
// we subtract 1 from the value so multiples of 100 are part of their number not the next highest.
int result = ((value - 1) / 100 ) + 1;
return result;
}
精彩评论