Decimal handling as value object instead of plain strings.

Related tags

Miscellaneous php
Overview

Decimal Object

Build Status codecov Latest Stable Version Minimum PHP Version PHPStan License

Decimal value object for PHP.

Background

When working with monetary values, normal data types like int or float are not suitable for exact arithmetic. Try out the following in PHP:

var_dump(0.1 + 0.2);        // float(0.3)
var_dump(0.1 + 0.2 - 0.3);  // float(5.5511151231258E-17)

Handling them as string is a workaround, but as value object you can more easily encapsulate some of the logic.

Alternatives

Solutions like https://php-decimal.io require a PHP extension (would make it faster, but also more difficult for some servers to be available). For details see the wiki.

Features

  • Super strict on precision/scale. Does not lose significant digits on its own. You need to trim() for this manually.
  • Speaking API (no le, gt methods).
  • Basic math operations and checks supported.
  • Immutability.
  • Handle very large and very small numbers.

Installation

Requirements

  • bcmath PHP extension enabled

Composer (preferred)

composer require spryker/decimal-object

Usage

See Documentation for more details.

Implementations

The following libraries are using the Decimal value object:

Comments
  • greatherThanOrEquals() Typo?

    greatherThanOrEquals() Typo?

    Hi. Thank you for the great package.

    I noticed that there is an extra "h" in the greatherThanOrEquals() function. By right it should be greaterThanOrEquals().

    It's not a big issue. Just thought I'd let you know anyway. :)

    Thanks again.

    improvement 
    opened by jianjye 2
  • Improvements for namespace and bootstrap setting

    Improvements for namespace and bootstrap setting

    PR Description

    • Removing tests/bootstrap.php file because it's empty and useless. It can set the vendor/autoload.php file inside phpunit.xml.dist file.
    • Changing the Spryker\DecimalObject\Test; namespace for test/DecimalTest class to be compatible with autoload-dev setting on composer.json file.

    Checklist

    • [x] I agree with the Code Contribution License Agreement in CONTRIBUTING.md
    improvement ready-for-release 
    opened by peter279k 1
  • Check for null value due to PHP8

    Check for null value due to PHP8

    patch release

    without this it fails in PHP due to its own type checks:

    Failed asserting that exception of type "TypeError" matches expected exception "InvalidArgumentException". Message was: "method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given" at decimal-object/src/Decimal.php:84

    bug ready-for-release 
    opened by dereuromark 0
  • Some improvements for next major release

    Some improvements for next major release

    I have seen your Repo and find it very useful. So I want to contribute some useful changes.

    Changes:

    • declare strict types
    • Added Return Types
    • Remove unnecessary consts
    • use regex group name for more readability
    • cast variables to string
    • fixed compatibility for PHP < 8.0 with substr in strict mode

    PR Description

    Done some code styles fixes and a bugfix with PHP < 8.0 in strict type mode

    Checklist

    • [X] I agree with the Code Contribution License Agreement in CONTRIBUTING.md
    opened by vansari 4
  • API todos

    API todos

    TODO/Questions

    • Internal state: normalized form as digits + exponent?
    • Rounding + ceil()/floor()
    • Assert/check edge case values (very small values)
    • sum(), average(), max(), min() as static methods ?
    • modulo()/power()?
    • shift()/toMagnitude($exponent) ?
    • API naming add() => plus(), subtract() => minus(), multiply() => multipliedBy/times(), divide() => devidedBy() ?

    Rounding Example:

    (string)$decimal->round(1); // '123.4'
    (string)$decimal->round(2); // '123.45'
    (string)$decimal->round(3); // '123.456'
    (string)$decimal->round(4); // '123.4560' (trailing zeroes are added)
    (string)$decimal->round(5); // '123.45600' (trailing zeroes are added)
    
    improvement 
    opened by dereuromark 1
Releases(1.0.2)
  • 1.0.2(Jul 30, 2021)

  • 1.0.1(Nov 5, 2020)

  • 1.0.0(Nov 25, 2019)

    Included commits: https://github.com/spryker/decimal-object/compare/0.1.0...1.0.0

    Improvements

    Final 1.0.0 stable release of this value object.

    Source code(tar.gz)
    Source code(zip)
  • 0.1.0(Oct 15, 2019)

    Initial Release

    First beta release of Decimal value object as open-source library.

    This value object can be used to deal with decimal values coming from "decimal" type DB columns instead of handling them as strings throughout a PHP project. (Value) objects have several advantages here over normal strings, one being a bit more strict with invalid values as well as providing convenience wrapper methods and syntactic sugar around common tasks and operations.

    Source code(tar.gz)
    Source code(zip)
Share value objects that contain the same primitive value as a singleton

sharable-value-objects Share value objects that contain the same primitive value as a singleton. Singletons are kept under WeakReference objects. Inst

mpyw 5 Nov 14, 2021
Strings Package provide a fluent, object-oriented interface for working with multibyte string

Strings Package provide a fluent, object-oriented interface for working with multibyte string, allowing you to chain multiple string operations together using a more readable syntax compared to traditional PHP strings functions.

Glowy PHP 14 Mar 12, 2022
PHP library that helps to map any input into a strongly-typed value object structure.

Valinor • PHP object mapper with strong type support Valinor is a PHP library that helps to map any input into a strongly-typed value object structure

Team CuyZ 873 Jan 7, 2023
Immutable value object for IPv4 and IPv6 addresses, including helper methods and Doctrine support.

IP is an immutable value object for (both version 4 and 6) IP addresses. Several helper methods are provided for ranges, broadcast and network address

Darsyn 224 Dec 28, 2022
Yet another Value Object Library (YAVOL)

Yet Another DDD Library Value object This library is a foundation in order to implement the Value Object pattern. It helps you to introduce some DDD s

YADDDL 3 Nov 17, 2022
Provides JSON pointer as a value object

json-pointer Provides JSON pointer as a value object. Installation Run composer require ergebnis/json-pointer Usage ReferenceToken You can create a Re

null 3 Dec 15, 2022
A PHP component to convert HTML into a plain text format

html2text html2text is a very simple script that uses DOM methods to convert HTML into a format similar to what would be rendered by a browser - perfe

Jevon Wright 423 Dec 29, 2022
The VarExporter component allows exporting any serializable PHP data structure to plain PHP code.

The VarExporter component allows exporting any serializable PHP data structure to plain PHP code. While doing so, it preserves all the semantics associated with the serialization mechanism of PHP (__wakeup, __sleep, Serializable).

Symfony 1.8k Jan 1, 2023
A plain-language, step-by-step guide for the computer novice to build their own cloud.

This is a plain-language, step-by-step guide for the computer novice wanting to build their own cloud, looking to declare independence from Google and its ilk, to save on monthly hosting fees, or just learn a new skill.

Paul Knight 25 Nov 19, 2022
An article about alternative solution for convert object into a JSON Object for your api.

Do we really need a serializer for our JSON API? The last years I did build a lot of JSON APIs but personally was never happy about the magic of using

Alexander Schranz 1 Feb 1, 2022
Your alter ego object. Takes the best of object and array worlds.

Supporting Opensource formapro\values is an MIT-licensed open source project with its ongoing development made possible entirely by the support of com

FormaPro 31 Jun 25, 2021
All PHP functions, rewritten to throw exceptions instead of returning false

Safe PHP This project is deprecated Because of how this project needs to be in sync with the official PHP documentation, maintaining a set of function

TheCodingMachine 2.1k Dec 28, 2022
All PHP functions, rewritten to throw exceptions instead of returning false, now for php8

A set of core PHP functions rewritten to throw exceptions instead of returning false when an error is encountered.

TheCodingMachine 106 Nov 21, 2022
Unicode-url-for-Textpattern - Plugin for using unicode urls instead of transliterations to ASCII characters

Unicode-url-for-Textpattern Summary textpattern plugin wcz_utf8_url – uses UTF-8 permlinks instead of transliterated ones for SEO Features automatical

Andrij 4 Dec 16, 2017
Enhancement to Magento to allow simple product prices to be used instead of the default special-case configurable product prices

Simple Configurable Products Extension For Magento This documentation applies to SCP versions 0.7 onwards. The documentation for SCP v0.6 and earlier

Simon King 288 Nov 7, 2022
LaraNx Seo enables your Laravel app to store SEO and social media meta tag data in database instead of your code

LaraNx Seo enables your Laravel app to store SEO and social media meta tag data in database instead of your code. Moving marketing data out of your code base and into your database where it is easily modified.

srg 13 Dec 29, 2022
Create executable strings using a fluent API.

command-builder A PHP class to build executable with using fluent API. Summary About Features Installation Examples Compatibility table Tests About I

Khalyomede 2 Jan 26, 2022
A comprehensive library for generating differences between two strings in multiple formats (unified, side by side HTML etc). Based on the difflib implementation in Python

PHP Diff Class Introduction A comprehensive library for generating differences between two hashable objects (strings or arrays). Generated differences

Chris Boulton 708 Dec 25, 2022
Shortest Path - have a function ShortestPath (strArr) take strArr which will be an array of strings which models a non-looping Graph.

Have the function ShortestPath(strArr) take strArr which will be an array of strings which models a non-looping Graph

null 1 Feb 5, 2022