https://github.com/pronamic/wp-number
WordPress Number library.
https://github.com/pronamic/wp-number
calculator decimal floating-point i18n library number php wordpress
Last synced: 6 months ago
JSON representation
WordPress Number library.
- Host: GitHub
- URL: https://github.com/pronamic/wp-number
- Owner: pronamic
- License: gpl-2.0
- Created: 2021-03-09T13:28:01.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-12-17T12:49:33.000Z (about 1 year ago)
- Last Synced: 2025-04-30T16:52:45.335Z (9 months ago)
- Topics: calculator, decimal, floating-point, i18n, library, number, php, wordpress
- Language: PHP
- Homepage:
- Size: 183 KB
- Stars: 4
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Pronamic WordPress Number
WordPress Number library.
[](https://packagist.org/packages/pronamic/wp-number)
[](https://packagist.org/packages/pronamic/wp-number)
[](https://packagist.org/packages/pronamic/wp-number)
[](https://packagist.org/packages/pronamic/wp-number)
[](https://codecov.io/gh/pronamic/wp-number)
[](https://coveralls.io/github/pronamic/wp-number?branch=main)
## Table of contents
- [Getting Started](#getting-started)
- [Usage](#usage)
- [Design Principles](#design-principles)
## Getting Started
### Installation
```
composer require pronamic/wp-number
```
## Usage
```
$number = Number::from_float( 123.50 )->add( 0.45 );
echo \esc_html( $number->format_i18n( 2 ) );
```
## Design Principles
### A number is a number
> In general, a number is a number, not a string, and this means that any programming language treats a number as a number. Thus, the number by itself doesn't imply any specific format (like using .000021 instead of 2.1e-5). This is nothing different to displaying a number with leading zeros (like 0.000021) or aligning lists of numbers. This is a general issue you'll find in any programming language: if you want a specific format you need to specify it, using the format functions of your programming language.
>
> Unless you specify the number as string and convert it to a real number when needed, of course. Some languages can do this implicitly.
https://stackoverflow.com/a/1471792
### Number in exponential form / scientific notation
> 2.1E-5 is the same number as 0.000021. That's how it prints numbers below 0.001. Use printf() if you want it in a particular format.
>
> **Edit** If you're not familiar with the `2.1E-5` syntax, you should know it is shorthand for 2.1×10-5. It is how most programming languages represent numbers in scientific notation.
https://stackoverflow.com/a/1471694
### Leading zeros
In https://github.com/moneyphp/money it is not allowed to use leading zeros:
> Leading zeros are not allowed
_Source:_ https://github.com/moneyphp/money/search?q=leading+zero
This probably has somehting to do with the following user note:
> Be careful with GMP - it considers leading zeros in a number string as meaning the number is in octal, whereas 'bc' doesn't:
>
> `gmp_strval("000100", 10) => 64`
>
> `bcmul("000100", "1") => 100`
_Source:_ https://www.php.net/manual/en/book.gmp.php#106521
```php
```
_Source:_ https://www.php.net/manual/en/language.types.integer.php
> A leading zero in a numeric literal means "this is octal". But don't be confused: a leading zero in a string does not. Thus:
> `$x = 0123; // 83`
> `$y = "0123" + 0 // 123`
_Source:_ https://www.php.net/manual/en/language.types.integer.php#111523
## Links
- https://0.30000000000000004.com/
[](https://www.pronamic.eu/contact/)