[New] added new formatting functions in StringHelper

- formatNanoDuration(long:nanos):String
- formatMillisecondsDuration(millis:long):String
This commit is contained in:
Robert von Burg 2012-11-16 09:58:52 +01:00
parent 2afba1876c
commit 0d392b1c60
2 changed files with 37 additions and 0 deletions

View File

@ -1,6 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.eitchnet</groupId>
<artifactId>ch.eitchnet.utils</artifactId>
<packaging>jar</packaging>

View File

@ -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$
}
}
}