PHPackages                             johndodev/price-formatter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. johndodev/price-formatter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

johndodev/price-formatter
=========================

Display / Format prices in PHP

v2.0(4y ago)134PHP

Since Mar 22Pushed 4y ago2 watchersCompare

[ Source](https://github.com/johndodev/price-formatter)[ Packagist](https://packagist.org/packages/johndodev/price-formatter)[ RSS](/packages/johndodev-price-formatter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

PriceFormatter
==============

[](#priceformatter)

Display prices in PHP.

Requirements
------------

[](#requirements)

PHP 5.4+

Usage
-----

[](#usage)

### Basic usage

[](#basic-usage)

Defaut settings are designed for Europeans, but you can change it, see *Default options* chapter.

```
use Johndodev\PriceFormatter;

// create an instance (see __construct chapter)
$priceFormatter = new PriceFormatter();

echo $priceFormatter->format(4);
// display "4 €"

echo $priceFormatter->format(4, 'USD');
// display 4 $

echo $priceFormatter->format(4, '$');
// display 4 $

echo $priceFormatter->format(4, 'USD')->symbolBefore()->symbolSep('');
// display $4
```

### Methods

[](#methods)

All methods are chainable:

```
echo $priceFormatter->format($numberToFormat, $currency = null)

// remove trailing zeros if necessary: 5.00 output 5 but 5.50 output 5.50)
->autoTrailingZeros(true)

// set the symbol separator
->symbolSep(' ')

// set the decimals separator
->decSep('.')

// set the maximum number of decimals to show
->decimals(2)

// put the symbol after the value
->symbolAfter()

// or before
->symbolBefore()

// but you can define the position with a variable
->symbolPosition(PriceFormatter::SYMBOL_POSITION_AFTER)

// set the thousands separator
->thousandsSep(',')

// remove trailing zeros (decimals), e.g.: 5.00 will output 5, 5.50 will output 5.5
->trimTrailingZeros(true)

// unbreakable spaces: replace " " by "&nbsp;"
->unbreakable(true);
```

### \_\_construct

[](#__construct)

You can create as many instances (aka services) as you want with their own defaut options (the next chapter is *Default options*)

```
PriceFormatter::__construct($options = [])

$euroFormatter = new PriceFormatter(['currency' => 'EUR']);
$euroFormatter = new PriceFormatter(['currency' => '€']);

$usdFormatter = new PriceFormatter([
    'currency' => 'USD',
    'symbolPosition' => PriceFormatter::SYMBOL_POSITION_BEFORE,
]);
```

### Default options

[](#default-options)

You can set all thoses settings per instance, below are default values:

```
$priceFormatter = new PriceFormatter([
    'currency'          => 'EUR',
    'decimals'          => 2,
    'decSep'            => '.',
    'thousandsSep'      => '',
    'symbolPosition'    => PriceFormatter::SYMBOL_POSITION_AFTER,
    'symbolSep'         => ' ',
    'unbreakable'       => true,
    'trimTrailingZeros' => false,
    'autoTrailingZeros' => true,
]);
```

Example:

```
// no spaces between currency and value
$priceFormatter = new PriceFormatter(['symbolSep' => '']);

// display 5€
echo $priceFormatter->format(5);

// but all options can be overriden for one format()
echo $priceFormatter->format(5)->symbolSep(' ');
// display 5 €

echo $priceFormatter->format(5);
// then display 5€ again
```

### Currencies and symbols

[](#currencies-and-symbols)

You can use these code `AUD` $, `CAD` $, `CHF` CHF, `CNY` ¥, `EUR` €, `GBP` £, `HKD` $, `JPY` ¥, `NOK` kr, `SEK` kr, `USD` $.

If you want to format an unsupported currency, use the Symbol ():

```
// 5 ฿
echo $priceFormatter->format(5, '฿');
```

Run tests
---------

[](#run-tests)

```
> composer install
> php vendor/bin/phpunit --testsuite PriceFormatter

```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2165 days

Total

2

Last Release

1534d ago

Major Versions

v1.0 → v2.02022-02-25

### Community

Maintainers

![](https://www.gravatar.com/avatar/020e5c8afb7cb01052109e43316d1623965e27ae44c917933a09c1f8fa51307b?d=identicon)[johndodev](/maintainers/johndodev)

---

Top Contributors

[![johndodev](https://avatars.githubusercontent.com/u/2741956?v=4)](https://github.com/johndodev "johndodev (3 commits)")

---

Tags

moneyformatdisplayprice

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/johndodev-price-formatter/health.svg)

```
[![Health](https://phpackages.com/badges/johndodev-price-formatter/health.svg)](https://phpackages.com/packages/johndodev-price-formatter)
```

###  Alternatives

[moneyphp/money

PHP implementation of Fowler's Money pattern

4.8k82.5M421](/packages/moneyphp-money)[willdurand/negotiation

Content Negotiation tools for PHP provided as a standalone library.

1.4k122.0M156](/packages/willdurand-negotiation)[akaunting/laravel-money

Currency formatting and conversion package for Laravel

7825.3M18](/packages/akaunting-laravel-money)[brick/money

Money and currency library

1.9k37.9M102](/packages/brick-money)[florianv/swap

Exchange rates library for PHP

1.3k6.4M16](/packages/florianv-swap)[cknow/laravel-money

Laravel Money

1.0k4.3M22](/packages/cknow-laravel-money)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
