Luminova Framework

PHP Luminova: Convert Units and Format Values

Last updated: 2026-11-28 20:57:12

Convert sizes, durations, distances, weights, and temperatures. Format decimal and currency amounts and restore binary data from a hex dump.

Use Converter to prepare values for display or convert quantities between known units. For decimal values that must retain their written precision, pass a numeric string to decimal(). Use Money for operations such as splitting or allocating totals.


Usage

use Luminova\Utility\Converter;

echo Converter::toUnit(1536, withName: true, trimZeros: true); // 1.5 KB
echo Converter::toBytes('1.5 MB'); // 1572864
echo Converter::temperature(68, 'F', 'C'); // 20

Class Definition

  • Full namespace: Luminova\Utility\Converter
  • This class is final.

Methods

toUnit

Scale bytes using powers of 1,024. Choose a spaced label or a compact Unix-style suffix.

public static function toUnit(
    float|int $bytes,
    int $decimals = 2,
    bool $withName = false,
    bool $unixName = false,
    bool $trimZeros = false
): string

Parameters:

ParameterTypeDescription
$bytesfloat\|intValue in bytes.
$decimalsintNumber of decimal places (default: 2).
$withNameboolWhether to append the unit (default: false).
$unixNameboolWhether to use a compact suffix when showing the unit (default: false).
$trimZerosboolWhether to trim trailing fractional zeros (default: false).

Return:

string - Formatted size.


toTimeUnit

Scale milliseconds to the largest supported unit reached by the duration. With the current implementation, trimZeros: true may also remove trailing zeros from whole numbers. Pass false to preserve them.

public static function toTimeUnit(
    float|int $milliseconds,
    int $decimals = 2,
    bool $withName = false,
    bool $withFullName = false,
    bool $trimZeros = true
): string

Parameters:

ParameterTypeDescription
$millisecondsfloat\|intDuration in milliseconds.
$decimalsintNumber of decimal places (default: 2).
$withNameboolWhether to append the unit (default: false).
$withFullNameboolWhether to use the full unit name when showing the unit (default: false).
$trimZerosboolWhether to trim trailing fractional zeros (default: true).

Return:

string - Formatted duration.


toBytes

Convert a size such as 1.5 MB to bytes using powers of 1,024. The input -1 returns PHP_INT_MAX.

public static function toBytes(string $size): int

Parameters:

ParameterTypeDescription
$sizestringSize string without a sign, optionally followed by a byte unit, or the sentinel -1.

Return:

int - Byte count, or 0 for an invalid string.


rating

Divide total rating points by the number of reviews.

public static function rating(
    int $reviews = 0,
    float $rating = 0,
    bool $round = false
): float

Parameters:

ParameterTypeDescription
$reviewsintNumber of reviews (default: 0).
$ratingfloatTotal rating points (default: 0).
$roundboolWhether to round the result to two decimal places (default: false).

Return:

float - Average rating, or 0.0 when the review count is zero.


money

Format a numeric amount with a period as the decimal separator and commas between thousands.

public static function money(
    mixed $amount,
    int $decimals = 2
): string

Parameters:

ParameterTypeDescription
$amountmixedAmount to format.
$decimalsintNumber of decimal places (default: 2).

Return:

string - Formatted amount, or 0.00 for a nonnumeric input.


currency

Format a currency amount with PHP Intl. When the locale is omitted, the application locale is used with an en-US fallback.

public static function currency(
    float $number,
    string $code = 'USD',
    ?string $locale = null
): string|false

Parameters:

ParameterTypeDescription
$numberfloatAmount to format.
$codestringCurrency code (default: 'USD').
$localestring\|nullLocale override (default: null).

Return:

string|false - Localized value, or false if formatting fails.


crypto

Format the decimal amount and append the uppercase currency code. The default precision follows the class currency table and falls back to eight for unlisted codes. Precision is clamped to 0–36.

public static function crypto(
    string|float|int $amount,
    string $currency = 'BTC',
    ?int $precision = null,
    bool $trimZeros = false
): string|false

Parameters:

ParameterTypeDescription
$amountstring\|float\|intNumeric amount.
$currencystringCurrency code (default: 'BTC').
$precisionint\|nullDecimal places override (default: null).
$trimZerosboolWhether to trim trailing fractional zeros (default: false).

Return:

string|false - Formatted amount and code, or false if decimal formatting fails.


decimal

Round a numeric value to the requested decimal places. Pass a string to preserve digits that a PHP float could lose.

public static function decimal(
    string|float|int $amount,
    int $decimals,
    bool $trimZeros = true
): string|false

Parameters:

ParameterTypeDescription
$amountstring\|float\|intNumeric value.
$decimalsintNumber of decimal places.
$trimZerosboolWhether to trim trailing fractional zeros (default: true).

Return:

string|false - Formatted decimal string, or false if the input cannot be parsed or the precision is negative.


discount

Subtract a percentage from the original value.

public static function discount(
    string|float|int $value,
    string|float|int $rate,
    ?int $precision = null
): float

Parameters:

ParameterTypeDescription
$valuestring\|float\|intOriginal value.
$ratestring\|float\|intDiscount percentage.
$precisionint\|nullDecimal places used for rounding. Pass null to skip explicit rounding (default: null).

Return:

float - Value after discount.

Throws:


interest

Add a percentage to the original value.

public static function interest(
    string|float|int $value,
    string|float|int $rate,
    ?int $precision = null
): float

Parameters:

ParameterTypeDescription
$valuestring\|float\|intOriginal value.
$ratestring\|float\|intInterest percentage.
$precisionint\|nullDecimal places used for rounding. Pass null to skip explicit rounding (default: null).

Return:

float - Value after the increase.

Throws:


percentage

Calculate the portion of a value represented by a percentage.

public static function percentage(
    string|float|int $rate,
    string|float|int $of,
    ?int $precision = null
): float

Parameters:

ParameterTypeDescription
$ratestring\|float\|intPercentage rate.
$ofstring\|float\|intBase value.
$precisionint\|nullDecimal places used for rounding. Pass null to skip explicit rounding (default: null).

Return:

float - Calculated portion.

Throws:


rate

Calculate a percentage of a value using the same calculation as percentage().

public static function rate(
    string|float|int $rate,
    string|float|int $of,
    ?int $precision = null
): float

Parameters:

ParameterTypeDescription
$ratestring\|float\|intPercentage rate.
$ofstring\|float\|intBase value.
$precisionint\|nullDecimal places used for rounding. Pass null to skip explicit rounding (default: null).

Return:

float - Calculated portion.

Throws:


distance

Convert between MM, CM, M, KM, IN, FT, YD, MI, and NMI. Units are case-insensitive.

public static function distance(
    float|int $value,
    string $from,
    string $to,
    int $precision = 2
): float

Parameters:

ParameterTypeDescription
$valuefloat\|intDistance value.
$fromstringSource unit.
$tostringTarget unit.
$precisionintNumber of decimal places (default: 2).

Return:

float - Converted distance. Identical units return the original value as a float.

Throws:


weight

Convert between MG, G, KG, T, OZ, LB, and ST. Units are case-insensitive.

public static function weight(
    float|int $value,
    string $from,
    string $to,
    int $precision = 2
): float

Parameters:

ParameterTypeDescription
$valuefloat\|intWeight value.
$fromstringSource unit.
$tostringTarget unit.
$precisionintNumber of decimal places (default: 2).

Return:

float - Converted weight. Identical units return the original value as a float.

Throws:


coordinates

Estimate great-circle distance using the Haversine formula. Supported output units are km, m, mi, nmi, yd, ft, and cm. Supply validated latitude and longitude values in degrees.

public static function coordinates(
    float|string $originLat,
    float|string $originLng,
    float|string $destLat,
    float|string $destLng,
    string $unit = 'km'
): float

Parameters:

ParameterTypeDescription
$originLatfloat\|stringOrigin latitude.
$originLngfloat\|stringOrigin longitude.
$destLatfloat\|stringDestination latitude.
$destLngfloat\|stringDestination longitude.
$unitstringOutput unit (default: 'km').

Return:

float - Estimated distance in the requested unit.

Throws:


temperature

Convert between Celsius (C), Fahrenheit (F), and Kelvin (K). Units are case-insensitive.

public static function temperature(
    float|int $value,
    string $from,
    string $to,
    int $precision = 2
): float

Parameters:

ParameterTypeDescription
$valuefloat\|intTemperature value.
$fromstringSource unit.
$tostringTarget unit.
$precisionintNumber of decimal places (default: 2).

Return:

float - Converted temperature. Identical units return the original value as a float.

Throws:


hexToBinary

Read hexadecimal bytes from the colon-prefixed data portion of each dump line. Lines without a matching data portion are ignored. An odd number of hex digits is padded on the left.

public static function hexToBinary(string $hexStr, ?string $destination = null): string|bool

Parameters:

ParameterTypeDescription
$hexStrstringHex dump text, such as 0000: 48 69.
$destinationstring\|nullOptional output file path (default: null).

Return:

string|bool - Binary bytes when no destination is supplied, or the file write status.

Throws:

Examples:

echo Converter::hexToBinary('0000: 48 69'); // Hi