Conversion

public final class Conversion

Provides a set of methods for unit conversion.

Author:Manoel Campos da Silva Filho

Fields

GIGA

public static final double GIGA

The value of 1 GigaByte in Bytes or 1 Gigabit in bits.

See also: .MEGA

HUNDRED_PERCENT

public static final double HUNDRED_PERCENT

A value that represents 100% in a scale from 0 to 1.

KILO

public static final double KILO

The value of 1 KiloByte in Bytes or 1 Kilobit in bits. It is declared as double because such a value is commonly used in divisions. This way, it avoids explicit double casts to ensure a double instead an integer division.

MEGA

public static final double MEGA

The value of 1 MegaByte in Bytes or 1 Megabit in bits.

See also: .KILO

MILLION

public static final int MILLION

One million in absolute value, usually used to convert to and from Number of Instructions (I) and Million Instructions (MI) units.

TERA

public static final double TERA

The value of 1 TeraByte in Bytes or 1 TeraBit in bits.

See also: .GIGA

Methods

bitesToBytes

public static double bitesToBytes(double bits)

Converts any value in bits to bytes, doesn’t matter if the unit is Kilobites (Kb), Megabites (Mb), Gigabites (Gb), etc.

Parameters:
  • bits – the value in bites, Kb, Mb, Gb, etc
Returns:

the value in bites, Kbytes, Mbytes, Gbytes and so on, according to the given value

bytesToBits

public static double bytesToBits(double bytes)

Converts any value in bytes to bits, doesn’t matter if the unit is Kilobytes (KB), Megabytes (MB), Gigabytes (GB), etc.

Parameters:
  • bytes – the value in bytes, KB, MB, GB, etc
Returns:

the value in bites, Kbits, Mbits, Gbits and so on, according to the given value

bytesToGigaBytes

public static double bytesToGigaBytes(double bytes)

Converts a value in bytes to GigaBytes (GB)

Parameters:
  • bytes – the value in bytes
Returns:

the value in GigaBytes (GB)

bytesToKiloBytes

public static double bytesToKiloBytes(double bytes)

Converts a value in bytes to KiloBytes (KB)

Parameters:
  • bytes – the value in bytes
Returns:

the value in KiloBytes (KB)

bytesToMegaBits

public static double bytesToMegaBits(double bytes)

Converts a value in bytes to Megabites (Mb)

Parameters:
  • bytes – the value in bytes
Returns:

the value in Megabites (Mb)

bytesToMegaBytes

public static double bytesToMegaBytes(double bytes)

Converts a value in bytes to MegaBytes (MB)

Parameters:
  • bytes – the value in bytes
Returns:

the value in MegaBytes (MB)

bytesToSuitableUnit

public static String bytesToSuitableUnit(double bytes)

Converts a value in bytes to the most suitable unit, such as Kilobytes (KB), MegaBytes (MB) or Gigabytes (GB)

Parameters:
  • bytes – the value in bytes
Returns:

the converted value concatenated with the unit converted to (KB, MB or GB)

gigaToMega

public static double gigaToMega(double giga)

Converts any value in giga to mega, doesn’t matter if it’s gigabits or gigabytes.

Parameters:
  • giga – the value in gigabits or gigabytes
Returns:

the value in megabits or megabytes (according to the input value)

megaBytesToBytes

public static double megaBytesToBytes(double megaBytes)

Converts a value in MegaBytes (MB) to bytes

Parameters:
  • megaBytes – the value in MegaBytes (MB)
Returns:

the value in bytes

microToMilli

public static double microToMilli(double micro)

Converts any value in micro (μ) to milli (m) scale, such as microseconds to milliseconds.

The existing java.util.concurrent.TimeUnit and java.time.Duration classes don’t provide the double precision required here.

Parameters:
  • micro – the value in micro (μ) scale
Returns:

the value in milli (m) scale

microToSeconds

public static double microToSeconds(double micro)

Converts any value in microseconds (μ) to seconds.

The existing java.util.concurrent.TimeUnit and java.time.Duration classes don’t provide the double precision required here.

Parameters:
  • micro – the value in microseconds (μ)
Returns:

the value in seconds

teraToGiga

public static double teraToGiga(double tera)

Converts any value in tera to giga, doesn’t matter if it’s terabits or terabytes.

Parameters:
  • tera – the value in terabits or terabytes
Returns:

the value in gigabits or gigabytes (according to the input value)

teraToMega

public static double teraToMega(double tera)

Converts any value in tera to mega, doesn’t matter if it’s terabits or terabytes.

Parameters:
  • tera – the value in terabits or terabytes
Returns:

the value in megabits or megabytes (according to the input value)