PHP Luminova: Convert Units and Format Values
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'); // 20Class 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
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$bytes | float\|int | Value in bytes. |
$decimals | int | Number of decimal places (default: 2). |
$withName | bool | Whether to append the unit (default: false). |
$unixName | bool | Whether to use a compact suffix when showing the unit (default: false). |
$trimZeros | bool | Whether 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
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$milliseconds | float\|int | Duration in milliseconds. |
$decimals | int | Number of decimal places (default: 2). |
$withName | bool | Whether to append the unit (default: false). |
$withFullName | bool | Whether to use the full unit name when showing the unit (default: false). |
$trimZeros | bool | Whether 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): intParameters:
| Parameter | Type | Description |
|---|---|---|
$size | string | Size 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
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$reviews | int | Number of reviews (default: 0). |
$rating | float | Total rating points (default: 0). |
$round | bool | Whether 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
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | mixed | Amount to format. |
$decimals | int | Number 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|falseParameters:
| Parameter | Type | Description |
|---|---|---|
$number | float | Amount to format. |
$code | string | Currency code (default: 'USD'). |
$locale | string\|null | Locale 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|falseParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | string\|float\|int | Numeric amount. |
$currency | string | Currency code (default: 'BTC'). |
$precision | int\|null | Decimal places override (default: null). |
$trimZeros | bool | Whether 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|falseParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | string\|float\|int | Numeric value. |
$decimals | int | Number of decimal places. |
$trimZeros | bool | Whether 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
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$value | string\|float\|int | Original value. |
$rate | string\|float\|int | Discount percentage. |
$precision | int\|null | Decimal places used for rounding. Pass null to skip explicit rounding (default: null). |
Return:
float - Value after discount.
Throws:
- Luminova\Exceptions\InvalidArgumentException if an input is nonnumeric, the rate is negative, or the supplied precision is negative.
interest
Add a percentage to the original value.
public static function interest(
string|float|int $value,
string|float|int $rate,
?int $precision = null
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$value | string\|float\|int | Original value. |
$rate | string\|float\|int | Interest percentage. |
$precision | int\|null | Decimal places used for rounding. Pass null to skip explicit rounding (default: null). |
Return:
float - Value after the increase.
Throws:
- Luminova\Exceptions\InvalidArgumentException if an input is nonnumeric, the rate is negative, or the supplied precision is negative.
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
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$rate | string\|float\|int | Percentage rate. |
$of | string\|float\|int | Base value. |
$precision | int\|null | Decimal places used for rounding. Pass null to skip explicit rounding (default: null). |
Return:
float - Calculated portion.
Throws:
- Luminova\Exceptions\InvalidArgumentException if an input is nonnumeric, the rate is negative, or the supplied precision is negative.
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
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$rate | string\|float\|int | Percentage rate. |
$of | string\|float\|int | Base value. |
$precision | int\|null | Decimal places used for rounding. Pass null to skip explicit rounding (default: null). |
Return:
float - Calculated portion.
Throws:
- Luminova\Exceptions\InvalidArgumentException if an input is nonnumeric, the rate is negative, or the supplied precision is negative.
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
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$value | float\|int | Distance value. |
$from | string | Source unit. |
$to | string | Target unit. |
$precision | int | Number of decimal places (default: 2). |
Return:
float - Converted distance. Identical units return the original value as a float.
Throws:
- Luminova\Exceptions\InvalidArgumentException if either unit is unsupported.
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
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$value | float\|int | Weight value. |
$from | string | Source unit. |
$to | string | Target unit. |
$precision | int | Number of decimal places (default: 2). |
Return:
float - Converted weight. Identical units return the original value as a float.
Throws:
- Luminova\Exceptions\InvalidArgumentException if either unit is unsupported.
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'
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$originLat | float\|string | Origin latitude. |
$originLng | float\|string | Origin longitude. |
$destLat | float\|string | Destination latitude. |
$destLng | float\|string | Destination longitude. |
$unit | string | Output unit (default: 'km'). |
Return:
float - Estimated distance in the requested unit.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the distance unit is unsupported.
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
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$value | float\|int | Temperature value. |
$from | string | Source unit. |
$to | string | Target unit. |
$precision | int | Number of decimal places (default: 2). |
Return:
float - Converted temperature. Identical units return the original value as a float.
Throws:
- Luminova\Exceptions\InvalidArgumentException if either unit is unsupported.
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|boolParameters:
| Parameter | Type | Description |
|---|---|---|
$hexStr | string | Hex dump text, such as 0000: 48 69. |
$destination | string\|null | Optional output file path (default: null). |
Return:
string|bool - Binary bytes when no destination is supplied, or the file write status.
Throws:
- Luminova\Exceptions\RuntimeException if matched hexadecimal data is invalid or the file-writing operation throws.
Examples:
echo Converter::hexToBinary('0000: 48 69'); // Hi