All Projects → madebybob → Php Number

madebybob / Php Number

Licence: mit
Deal with numbers the right way in PHP

Projects that are alternatives of or similar to Php Number

Css In Js Benchmarks
Stars: ✭ 360 (+309.09%)
Mutual labels:  calculations
Numgen
Creates objects that generate number sequences
Stars: ✭ 5 (-94.32%)
Mutual labels:  number
Quantities
A library to work with quantities and units.
Stars: ✭ 32 (-63.64%)
Mutual labels:  calculations
React Phone Input 2
📞 Highly customizable phone input component with auto formatting
Stars: ✭ 446 (+406.82%)
Mutual labels:  number
Sudoku
Can Neural Networks Crack Sudoku?
Stars: ✭ 742 (+743.18%)
Mutual labels:  number
Numericaltextentry
An iOS library for beautiful number entry fields. iPad friendly. Written in Swift.
Stars: ✭ 16 (-81.82%)
Mutual labels:  number
React Native Phone Verification
The best React Native example for phone verification (an alternative to Twitter Digits).
Stars: ✭ 332 (+277.27%)
Mutual labels:  number
Spincounterview
🎡 一个类似于码表变化的旋转计数器动画控件
Stars: ✭ 47 (-46.59%)
Mutual labels:  number
Yasumi
The easy PHP Library for calculating holidays
Stars: ✭ 788 (+795.45%)
Mutual labels:  calculations
React Native Number Please
🔢 Generate react-native pickers with range numbers.
Stars: ✭ 30 (-65.91%)
Mutual labels:  number
Rollingtext
Android TextView with rolling animation
Stars: ✭ 494 (+461.36%)
Mutual labels:  number
React Phone Number Input
React component for international phone number input
Stars: ✭ 725 (+723.86%)
Mutual labels:  number
Translatedjs
Internationalization and localization for JavaScript and Node.js
Stars: ✭ 17 (-80.68%)
Mutual labels:  number
Vue Phone Number Input
A phone number input made with Vue JS (format & valid phone number)
Stars: ✭ 407 (+362.5%)
Mutual labels:  number
React Pivot
React-Pivot is a data-grid component with pivot-table-like functionality for data display, filtering, and exploration.
Stars: ✭ 981 (+1014.77%)
Mutual labels:  calculations
Ik
Minimal Inverse Kinematics library
Stars: ✭ 340 (+286.36%)
Mutual labels:  calculations
Mobile Select
手机移动端选择组件 支持是否级联/单选到多选/可异步更新数据等..
Stars: ✭ 829 (+842.05%)
Mutual labels:  number
Happy Captcha
Happy Captcha是一款易于使用的Java验证码软件包,旨在花最短的时间,最少的代码量,实现Web站点的验证码功能。Happy Captcha完全遵循Apache 2.0开源许可协议,你可以自由使用该软件,如您在使用Happy Captcha时发现软件的任何缺陷,欢迎随时与我联系。
Stars: ✭ 75 (-14.77%)
Mutual labels:  number
Period
Complex period comparisons
Stars: ✭ 1,001 (+1037.5%)
Mutual labels:  calculations
Codice Fiscale
Javascript object for managing the italian tax code
Stars: ✭ 9 (-89.77%)
Mutual labels:  calculations

PHP Number - Deal with numbers the right way

GitHub Workflow Status (branch) Latest Version on Packagist Total Downloads GitHub

PHP Number

This library aims to deal with numbers like prices, weights and quantities the right way in PHP.

The problem

Have you ever worked with prices, weights, or any other numbers in PHP? What type are they? An integer? A string? Or did you get a float to manage decimals? And how can you do calculations with them? We have all struggled with float's counter-intuitive behavior.

Ahh, after hours of investigation you've found BC Math. Now you can do math with your numbers. However, it is still hard to manage those numbers in your codebase. BC Math only accepts and returns strings. Type hinting strings when working with numbers as a modern-php-techie is not done.

The solution

This library will help you to manage number in your codebase. Using the Number class you can typehint them (getTotal(Number $quantity)) and make calculations on the number itself ($number->sum('200')'). Since those methods are immutable you can chain your methods on them.

Scope

This library aims to make your code cleaner. You can typehint the Number class, and you can make cleaner calculations (still using BC Math in the background).

We have chosen not to support specific implementations of numbers like money and weights. This is too specific and out of scope. Mainly, custom implementations of numbers are business specific. In our opinion you should create them yourself, according to the desired needs of your business.

Table of Contents

Installation

You can install the package via composer:

composer require madebybob/php-number

Usage

In short, the Number\Number class is where it's all about. This is how you can create a Number instance:

use Number\Number;

// with string
$number = new Number('200');

// with integer
$number = new Number(200);

// with float
$number = new Number(200.8);

// via static method
$quantity = Number::create(4);

// calculations
$total = $number->add($quantity);

Creating new numbers (or using them in the methods below) supports types like Number, string, integer and float.

Add

To add a new number to your current number instance:

$total = $number
    ->add('200')
    ->plus('200');

Subtract

To subtract a number from your current number instance:

$total = $number
    ->subtract('200')
    ->sub('200') // sub is an alias for subtract
    ->minus('200'); // minus is an alias for subtract

Divide

To divide your current number instance into the given number:

$total = $number
    ->divide('200')
    ->div('200'); // div is an alias for divide

Division by zero is not possible, of course. To not break your chain, a fallback value can be used like this:

$total = $number->divide($variable, null, '1.000');

Multiply

To multiply your current number instance with the given number:

$total = $number
    ->multiply('200')
    ->mul('200'); // mul is an alias for multiply

Modulus

To get the modulus of the current number instance:

$newNumber = $number
    ->modulus('200')
    ->mod('200'); // mod is an alias for modulus

Square root

To get the square root of the current number instance:

$sqrt = $number
    ->sqrt()
    ->squareRoot(); // squareRoot is an alias for sqrt

State & Comparison

To compare two numbers with each other, these helpers are available, which will return a bool:

$number = new Number('200');

// check if the number is positive
$number->isPositive();

// check if the number is negative
$number->isNegative();

// check if the number is greater than x
$number->isGreaterThan('100');

// check if the number is less than x
$number->isLessThan('300');

// check if the number is equal to x
$number->isEqual('200');

// check if the number is zero ("0")
$number->isZero();

Absolute & opposite values

To get the absolute (positive) value of the current instance:

$number = new Number('-200');

// $absolute will be 200
$absolute = $number->absolute();

// abs is an alias for absolute
$abs = $number->abs();

To get the opposite value of the current instance:

$number = new Number('200');

// $absolute will be -200
$absolute = $number->opposite();

// opp is an alias for absolute
$abs = $number->opp();

Rounding

To round the current number instance, the following methods are available:

$number = new Number('200.5000');

// rounds the number to '201.0000'
$number->round();

// ceils the number to '201.0000'
$number->ceil();

// floors the number to '200.0000'
$number->floor();

Immutable & Chaining

Since the Number class is immutable, most methods will return a new Number instance.

$two = new Number(2);
$four = $two->plus(2);

echo $two->toString(); // $two is still 2

Because the mathematical methods are fluent, you will be able to chain your calculations like so:

$number = new Number('200');

$result = $number
    ->add(200)
    ->subtract(109.5)
    ->mul($two)
    ->toString();

Extensibility

We encourage you to create custom implementations of the AbstractNumber class for your specific use cases. This enables you to type hint much better which type of number you expect and how they should be formatted.

PHP Number - Examples is a repository dedicated to showing how a custom number type like weights should be implemented. Please check out this repository for further documentation about the extensibility of this package.

Testing & Php CS Fixer

composer test
./vendor/bin/php-cs-fixer fix

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Supporters

Stargazers repo roster for @madebybob/php-number

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].