I need to separate an integer into two numbers. Something like dividing by two but I only want integer components as a result, such as:
6 = 3 and 3
7 = 4 and 3
I tried the following, but I'm not sure its the best solution.
var number = 7;
var part1 = 0;
var part2 = 0;
if((number % 2) == 0) {
part1 = number / 2;
part2 = number / 2;
}
else {
part1 = parseInt((number / 2) + 1);
part2 = parseInt(number / 2);
}
This does what I want, but I don't think this code is clean.
Are there better ways of doing this?
question from:
https://stackoverflow.com/questions/45164645/separate-an-integer-into-two-nearly-equal-parts 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…