From a210fb8dabea990b497f1a1a7dda6649842c9145 Mon Sep 17 00:00:00 2001 From: mdb Date: Wed, 18 Jan 2006 18:29:28 +0000 Subject: [PATCH] Added percent() which also handles 0/0. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1763 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../src/java/com/samskivert/velocity/DataTool.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/projects/samskivert/src/java/com/samskivert/velocity/DataTool.java b/projects/samskivert/src/java/com/samskivert/velocity/DataTool.java index 81a2202e..650271d0 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/DataTool.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/DataTool.java @@ -45,6 +45,15 @@ public class DataTool return (array == null) ? 0 : Array.getLength(array); } + /** + * Returns the numerator as a percentage of the denominator (100 * num / + * denom), handles 0/0 (returns 0), but will div0 on N/0 where N != 0. + */ + public int percent (int num, int denom) + { + return (num == 0) ? 0 : 100 * num / denom; + } + /** * Floating point divide. */