pwweb / copper
An API extension for NumberFormatter.
Requires
- php: ^8.2
- ext-intl: *
- laravel/framework: ^11.0||^12.0
Requires (Dev)
- laravel/pint: ^1.20
- pestphp/pest: ^v3.7.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- rector/rector: ^2.0
- squizlabs/php_codesniffer: ^3.4
- thecodingmachine/safe: 3.0.0-alpha1 as 2.5.0
This package is auto-updated.
Last update: 2025-03-25 14:40:55 UTC
README
Copper: An API extension for NumberFormatter.
Installation
Via Composer run the following:
$ composer require pwweb/copper
Usage
Commands
This package makes it easy to format numbers using PHP NumberFormatter
. In essence it wraps things up in a simple to use API.
To get started you will need to create a new instance:
<?php ... Copper\Copper::create(?float $value = null, ?int $style = null, ?string $locale = null);
i.e.:
<?php ... $formatter = Copper\Copper::create(-1234.56, \NumberFormatter::DECIMAL, 'en-GB');
If you don't provide a $style
it will default to NumberFormatter::DECIMAL
. If you don't provide the $locale
it will default to the value of the Laravel application.
Then with this you can format in any of the following ways:
Format | Code | Output |
---|---|---|
Currency | $value->currency('GBP') |
-£1,234.56 |
Decimal | $value->decimal(2) |
-1,234.56 |
Percentage | $value->percentage() |
-123,456% |
Scientific | $value->scientific() |
-1.23456E3 |
Accounting | $value->accounting('GBP') |
(£1,234.56) |
SpellOut | $value->spellOut() |
minus one thousand two hundred thirty-four point five six |
Unit | $value->unit(Unit::GRAM) |
1.234 kg |
Since the create()
function returns an instance of Copper
, you can chain the methods together i.e. Copper\Copper::create(-1234.56)->currency('EUR')
leads to -€1,234.56
.
This means that it's particularly helpful in Blade templates:
{{ Copper\Copper::create($order->value)->currency('USD') }}
In addition to the base functions there are a few setters and getters:
Function | Result |
---|---|
setStyle(int) |
Allows for altering the style value. |
getstyle(): int |
Returns the style number currently defined. |
setLocale(string) |
Allows for changing/specifying the locale within the code i.e. {{ Copper\Copper::create(-1234.56)->setLocale('de-DE')->currency('EUR') }} would return -1.234,56 € . |
Change log
Please see the changelog for more information on what has changed recently.
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email securtity@pw-websolutions.com instead of using the issue tracker.
Credits
License
Copyright © pw-websolutions.com. Please see the license file for more information.
˜