Luminova Framework

PHP Luminova: Basic Mathematical Operations Helper with the Math Class

Last updated: 2024-08-28 23:36:09

This class has basic mathematical calculation methods to convert currency, units, averages, ratings, discounts or interest rates.

The Maths module offers a suite of essential mathematical functions to support your development needs. It includes fundamental methods for tasks such as currency and unit conversions, calculating discounts, interest, and more. These tools are designed to simplify common mathematical operations, with plans to expand functionality for advanced use cases in the future.

  • Class namespace: \Luminova\Functions\Maths
  • This class is marked as final and can't be subclassed

Methods

The methods can be accessed through global function helper, Factory instance or call statically.

<?php
func('math')->money(5000);
// 5000.00

Or directly call math method.

<?php
func()->math()->money(5000);
// 5000.00

Or using factory instance to initialize function and call math method.

<?php
use Luminova\Application\Factory;

Factory::functions()->math()->money(5000);
// 5000.00

Or continently call it statically.

<?php
use \Luminova\Functions\Maths;

Maths::money(5000);

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: false).

Return Value:

string - Return 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 - Return the size in bytes.


average

Calculate the average of a giving numbers.

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

Parameters:

ParameterTypeDescription
$numbersint|floatThe 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 - Return 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
$reviewsintThe total number of reviews.
$ratingfloatThe total sum of rating points.
$roundboolWhether to round the average to 2 decimal places (default: false).

Return Value:

float - Return the average rating.

Example

The average of the below rating is: 8.50.

<?php 
echo Math::rating(5, 42.5, true)

money

Formats currency with decimal places and comma separation.

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

Parameters:

ParameterTypeDescription
$amountmixedThe amount you want to format.
$decimalsintThe decimals places.

Return Value:

string - Return 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 - Return the formatted currency string, or false if unable to format.


crypto

Format a number to it's cryptocurrency length.

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

Parameters:

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

Return Value:

string|false - The equivalent amount in cryptocurrency.

Available Cryptos

  • BTC - Bitcoin.
  • ETH - Ethereum.
  • LTC - Litecoin.
  • XRP - Ripple Credits.
  • DOGE - Doge Coin.

distance

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

public static distance(float|string $origin_lat, float|string $origin_lng, float|string $dest_lat, float|string $dest_lng, string $unit = 'km'): float|false

Parameters:

ParameterTypeDescription
$origin_latfloat|stringThe latitude of the origin point.
$origin_lngfloat|stringThe longitude of the origin point.
$dest_latfloat|stringThe latitude of the destination point.
$dest_lngfloat|stringThe longitude of the destination point.
$unitstringThe unit of distance to be returned (default is 'km').

Return Value:

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

Supported units:

  • 'km' - Kilometers,
  • 'mi' - Miles,
  • 'nmi' - Nautical miles.

Note: 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 - Return the formatted and rounded number.


discount

Calculate the discounted amount based on rate given.

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 - Return the discounted amount.


interest

Calculate the total amount after adding an interest based on rete.

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 - Return he total amount with an interest rate.