Luminova Framework

Maths Helper

Last updated: 2024-04-29 22:20:25

This class provides a set of fundamental mathematical calculation methods currency conversions and unit conversions. They are designed to assist developers in performing common mathematical tasks while we add more for advance usages.


  • Class namespace: \Luminova\Functions\Maths

Methods

toUnit

Converts bytes to the appropriate unit.

public static toUnit(int $bytes, bool $add_name = false): string

Parameters:

ParameterTypeDescription
$bytesintThe number of bytes to convert.
$add_nameboolWhether to include the unit name in the result. Default is false.

Return Value:

string - The converted value with optional unit name.


toBytes

Converts a unit name to bytes.

public static toBytes(string $units): int

Parameters:

ParameterTypeDescription
$unitsstringThe string representation of the byte size (e.g., '1KB', '2MB').

Return Value:

int - The size in bytes.


average

Calculate the average of a giving numbers.

public static average(int|float $numbers): float|null

Parameters:

ParameterTypeDescription
$numbersint|floatInput arguments integers or float values to calculate the average
- @example average(10, 20, 30, 40, 50) - return 30 as the average.

Return Value:

float|null - The average of the passed numbers.


rating

Calculate the average rating based on the number of reviews and total rating points.

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

Parameters:

ParameterTypeDescription
$reviewsintTotal number of reviews.
$ratingfloatTotal sum of rating points.
$roundboolWhether to round the average to 2 decimal places.

- @example averageRating(5, 42.5, true) The average rating is: 8.50

Return Value:

float - The average rating.


money

Formats currency with decimal places and comma separation.

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

Parameters:

ParameterTypeDescription
$amountmixedAmount you want to format.
$decimalsboolDecimals places.

Return Value:

string - Formatted currency string.


currency

Format a number as a currency string using your application local as the default currency locale.

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

Parameters:

ParameterTypeDescription
$numberfloatThe number to format.
$codestringThe currency code (optional).
$localestring|nullTOptional pass locale name to use in currency formatting.

Return Value:

string|false - The formatted currency string, or false if unable to format.


crypto

Convert a number to cryptocurrency.

public static crypto(int|float|string $amount, string $network = 'BTC'): float|false

Parameters:

ParameterTypeDescription
$amountint|float|stringThe amount to convert.
$networkstringThe cryptocurrency code (e.g., 'BTC', 'ETH', 'LTC').

Return Value:

float|false - The equivalent amount in cryptocurrency.


distance

Calculate the distance between two points on the Earth's surface.

public static distance(float|string $olat, float|string $olon, float|string $dlat, float|string $dlon, string $unit = 'km'): float|false

Parameters:

ParameterTypeDescription
$olatfloat|stringThe latitude of the origin point.
$olonfloat|stringThe longitude of the origin point.
$dlatfloat|stringThe latitude of the destination point.
$dlonfloat|stringThe longitude of the destination point.
$unitstringThe unit of distance to be returned (default is 'km').
Supported units: 'km', 'mi', 'nmi'.

Return Value:

float|false - The distance between the two points, or false on invalid input.

If you are passing a string, make sure its a float string.


fixed

Format a number with optional rounding.

public static fixed(float|int|string $number, int|null $decimals = null): string

Parameters:

ParameterTypeDescription
$numberfloat|int|stringThe number you want to format.
$decimalsint|nullThe number of decimal places (null for no rounding).

Return Value:

string - The formatted number.


discount

Calculate the discounted amount.

public static discount(float|int|string $total, float|int|string $rate): float

Parameters:

ParameterTypeDescription
$totalfloat|int|stringThe total amount you want to discount.
$ratefloat|int|stringThe discount rate (percentage) as an integer.

Return Value:

float - The discounted amount.


interest

Calculate the total amount after adding interest.

public static interest(float|int|string $total, float|int|string $rate): float

Parameters:

ParameterTypeDescription
$totalfloat|int|stringThe amount to which interest will be added.
$ratefloat|int|stringThe interest rate as a percentage (float or int).

Return Value:

float - The total amount after adding interest.