1 package org.unicode.cldr.util; 2 3 public class CompletionPercent { 4 /** 5 * Calculate a "user-friendly" percentage value between 0 and 100 6 * 7 * @param done the number of tasks that have been completed 8 * @param total the total number of tasks 9 * @return a number between 0 and 100 10 */ calculate(int done, int total)11 public static int calculate(int done, int total) { 12 if (total <= 0) { 13 // The task is finished since nothing needed to be done 14 // Do not divide by zero 15 return 100; 16 } 17 if (done <= 0) { 18 return 0; 19 } 20 // Do not round 99.9 up to 100 21 final int floor = (int) Math.floor((100 * (float) done) / (float) total); 22 if (floor == 0) { 23 // Do not round 0.001 down to zero 24 // Instead, provide indication of slight progress 25 return 1; 26 } 27 return Math.min(floor, 100); 28 } 29 } 30