Package oshi.util

Class FormatUtil

java.lang.Object
oshi.util.FormatUtil

@ThreadSafe
public final class FormatUtil
extends java.lang.Object
Formatting utility for appending units or converting between number types.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static java.lang.String HEX_ERROR
    Constant HEX_ERROR="0x%08X"
  • Method Summary

    Modifier and Type Method Description
    static java.lang.String formatBytes​(long bytes)
    Format bytes into a rounded string representation using IEC standard (matches Mac/Linux).
    static java.lang.String formatBytesDecimal​(long bytes)
    Format bytes into a rounded string representation using decimal SI units.
    static java.lang.String formatElapsedSecs​(long secs)
    Formats an elapsed time in seconds as days, hh:mm:ss.
    static java.lang.String formatError​(int errorCode)
    Translate an integer error code to its hex notation
    static java.lang.String formatHertz​(long hertz)
    Format hertz into a string to a rounded string representation.
    static java.lang.String formatValue​(long value, java.lang.String unit)
    Format arbitrary units into a string to a rounded string representation.
    static long getUnsignedInt​(int x)
    Convert unsigned int to signed long.
    static float round​(float d, int decimalPlace)
    Round to certain number of decimals.
    static java.lang.String toUnsignedString​(int i)
    Represent a 32 bit value as if it were an unsigned integer.
    static java.lang.String toUnsignedString​(long l)
    Represent a 64 bit value as if it were an unsigned long.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • formatBytes

      public static java.lang.String formatBytes​(long bytes)
      Format bytes into a rounded string representation using IEC standard (matches Mac/Linux). For hard drive capacities, use @link formatBytesDecimal(long). For Windows displays for KB, MB and GB, in JEDEC units, edit the returned string to remove the 'i' to display the (incorrect) JEDEC units.
      Parameters:
      bytes - Bytes.
      Returns:
      Rounded string representation of the byte size.
    • formatBytesDecimal

      public static java.lang.String formatBytesDecimal​(long bytes)
      Format bytes into a rounded string representation using decimal SI units. These are used by hard drive manufacturers for capacity. Most other storage should use formatBytes(long).
      Parameters:
      bytes - Bytes.
      Returns:
      Rounded string representation of the byte size.
    • formatHertz

      public static java.lang.String formatHertz​(long hertz)
      Format hertz into a string to a rounded string representation.
      Parameters:
      hertz - Hertz.
      Returns:
      Rounded string representation of the hertz size.
    • formatValue

      public static java.lang.String formatValue​(long value, java.lang.String unit)
      Format arbitrary units into a string to a rounded string representation.
      Parameters:
      value - The value
      unit - Units to append metric prefix to
      Returns:
      Rounded string representation of the value with metric prefix to extension
    • formatElapsedSecs

      public static java.lang.String formatElapsedSecs​(long secs)
      Formats an elapsed time in seconds as days, hh:mm:ss.
      Parameters:
      secs - Elapsed seconds
      Returns:
      A string representation of elapsed time
    • round

      public static float round​(float d, int decimalPlace)
      Round to certain number of decimals.
      Parameters:
      d - Number to be rounded
      decimalPlace - Number of decimal places to round to
      Returns:
      rounded result
    • getUnsignedInt

      public static long getUnsignedInt​(int x)
      Convert unsigned int to signed long.
      Parameters:
      x - Signed int representing an unsigned integer
      Returns:
      long value of x unsigned
    • toUnsignedString

      public static java.lang.String toUnsignedString​(int i)
      Represent a 32 bit value as if it were an unsigned integer. This is a Java 7 implementation of Java 8's Integer.toUnsignedString.
      Parameters:
      i - a 32 bit value
      Returns:
      the string representation of the unsigned integer
    • toUnsignedString

      public static java.lang.String toUnsignedString​(long l)
      Represent a 64 bit value as if it were an unsigned long. This is a Java 7 implementation of Java 8's Long.toUnsignedString.
      Parameters:
      l - a 64 bit value
      Returns:
      the string representation of the unsigned long
    • formatError

      public static java.lang.String formatError​(int errorCode)
      Translate an integer error code to its hex notation
      Parameters:
      errorCode - The error code
      Returns:
      A string representing the error as 0x....