From 0d392b1c602acc10b97d2f863b8d1f7e0f415c31 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Fri, 16 Nov 2012 09:58:52 +0100 Subject: [PATCH] [New] added new formatting functions in StringHelper - formatNanoDuration(long:nanos):String - formatMillisecondsDuration(millis:long):String --- pom.xml | 1 + .../eitchnet/utils/helper/StringHelper.java | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/pom.xml b/pom.xml index ffcf01466..20471eb4a 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,7 @@ 4.0.0 + ch.eitchnet ch.eitchnet.utils jar diff --git a/src/main/java/ch/eitchnet/utils/helper/StringHelper.java b/src/main/java/ch/eitchnet/utils/helper/StringHelper.java index c69de70d0..5911a4693 100644 --- a/src/main/java/ch/eitchnet/utils/helper/StringHelper.java +++ b/src/main/java/ch/eitchnet/utils/helper/StringHelper.java @@ -413,4 +413,40 @@ public class StringHelper { return sb.toString(); } + + /** + * Formats the given number of milliseconds to a time like 0.000s/ms + * + * @param millis + * the number of milliseconds + * + * @return format the given number of milliseconds to a time like 0.000s/ms + */ + public static String formatMillisecondsDuration(final long millis) { + if (millis > 1000) { + return String.format("%.3fs", (((double) millis) / 1000)); //$NON-NLS-1$ + } + + return millis + "ms"; //$NON-NLS-1$ + } + + /** + * Formats the given number of nanoseconds to a time like 0.000s/ms/us/ns + * + * @param nanos + * the number of nanoseconds + * + * @return format the given number of nanoseconds to a time like 0.000s/ms/us/ns + */ + public static String formatNanoDuration(final long nanos) { + if (nanos > 1000000000) { + return String.format("%.3fs", (((double) nanos) / 1000000000)); //$NON-NLS-1$ + } else if (nanos > 1000000) { + return String.format("%.3fms", (((double) nanos) / 1000000)); //$NON-NLS-1$ + } else if (nanos > 1000) { + return String.format("%.3fus", (((double) nanos) / 1000)); //$NON-NLS-1$ + } else { + return nanos + "ns"; //$NON-NLS-1$ + } + } }