Skip to main content

Price

A Price represents a currency-independent amount in minor units (e.g. cents). Internally, the amount is stored as a string and uses BCMath, so prices never suffer from floating point rounding errors.

Creating a price

To create a Price, you pass an integer in minor units (e.g. cents) to the of method:

1use Larasell\Larasell\Price;
2
3Price::of(1_000); // Amount in cents
4Price::of('1000'); // Strings are also allowed

Use fromArray if you need to create a Price from a payload/array:

1use Larasell\Larasell\Price;
2
3Price::fromArray(['amount' => 1299]);

Passing anything other than an integer value throws an InvalidArgumentException. This includes decimal or formatted strings:

1use Larasell\Larasell\Price;
2
3Price::of('12.99'); // throws InvalidArgumentException
4Price::of('1_000'); // throws InvalidArgumentException

Larasell normalizes price amounts. Leading zeros are stripped, and a negative zero becomes a positive zero:

1use Larasell\Larasell\Price;
2
3Price::of('04999')->amount(); // returns '4999'
4Price::of('-0')->amount(); // returns '0'

Access & serialization

The amount method returns the value as a string:

1use Larasell\Larasell\Price;
2
3$price = Price::of(4999);
4$price->amount(); // returns '4999'

To serialize prices, you can use the toArray and jsonSerialize methods:

1use Larasell\Larasell\Price;
2
3// Both return ['amount' => '4999']
4$price->toArray();
5$price->jsonSerialize();

Arithmetic operations

You can do a bunch of mathematical calculations with prices, like adding two prices:

1use Larasell\Larasell\Price;
2
3$first = Price::of(800);
4$second = Price::of(1000);
5
6$result = $first->add($second);
7$result->amount(); // returns '1800'

Or subtracting them:

1use Larasell\Larasell\Price;
2
3$first = Price::of(600);
4$second = Price::of(300);
5
6$result = $first->subtract($second);
7$result->amount(); // returns '300'

Subtracting prices can result in negative amounts:

1use Larasell\Larasell\Price;
2
3$first = Price::of(400);
4$second = Price::of(800);
5
6$result = $first->subtract($second);
7$result->amount(); // returns '-400'

You can also multiply prices:

1use Larasell\Larasell\Price;
2
3$price = Price::of(200);
4
5$result = $price->multiply(5);
6$result->amount(); // returns '1000'

There's also the greaterThan method to compare prices:

1use Larasell\Larasell\Price;
2
3$first = Price::of(100);
4$second = Price::of(200);
5
6$first->greaterThan($second); // returns false
7$second->greaterThan($first); // returns true

Here's the full set of comparison methods: greaterThan, greaterThanOrEqual, lessThan, lessThanOrEqual and equals:

1use Larasell\Larasell\Price;
2
3$first = Price::of(100);
4$second = Price::of(200);
5
6$first->lessThan($second); // returns true
7$first->lessThanOrEqual(Price::of(100)); // returns true
8$second->greaterThanOrEqual($first); // returns true
9$first->equals(Price::of(100)); // returns true

If you need the raw comparison result, compareTo returns -1 when the price is less than the given price, 0 when they are equal and 1 when it is greater:

1use Larasell\Larasell\Price;
2
3Price::of(100)->compareTo(Price::of(200)); // returns -1
4Price::of(200)->compareTo(Price::of(100)); // returns 1
5Price::of(100)->compareTo(Price::of(100)); // returns 0

Note: comparisons only consider the amount, not the currency. A price of 100 EUR cents is "equal" to a price of 100 USD cents. Since prices are currency-independent, you are responsible for only comparing prices in the same currency.

isPositive, isNegative and isZero allow you to check whether a price is positive, negative or zero:

1use Larasell\Larasell\Price;
2
3Price::of(-500)->isPositive(); // returns false
4Price::of(100)->isPositive(); // returns true
5Price::of(-500)->isNegative(); // returns true
6Price::of(0)->isZero(); // returns true

Formatting prices

To display a price, pass it to the format method together with a currency. The currency can be a string or a Currency enum case. Minor units are respected per currency, so JPY has no decimals, BHD uses three and EUR uses two:

1use Larasell\Larasell\Enums\Currency;
2use Larasell\Larasell\Price;
3
4$price = Price::of(1299);
5
6Price::format($price, 'EUR'); // '€12.99' (locale dependent)
7Price::format($price, Currency::EUR); // '€12.99'
8Price::format($price, Currency::JPY); // '¥1,299'

Pass a locale as the third argument to format the price for a specific locale. Either as a simple string or a Locale enum case. If you leave the parameter out, Larasell formats the price determined by App::currentLocale():

1use Larasell\Larasell\Enums\Currency;
2use Larasell\Larasell\Enums\Locale;
3use Larasell\Larasell\Price;
4
5Price::format($price, Currency::EUR, 'de-DE'); // '12,99 €'
6Price::format($price, Currency::EUR, Locale::ENGLISH_UNITED_STATES); // '€12.99'

Currencies

The Currency enum represents currencies. Every case knows its minor unit digits, which the formatter uses to convert minor units to the displayed amount. Most currencies use two decimals, but some use three (like BHD, KWD, OMR) and some have no minor unit at all (like JPY, ISK, VND):

1use Larasell\Larasell\Enums\Currency;
2
3Currency::USD->minorUnitDigits(); // 2
4Currency::BHD->minorUnitDigits(); // 3
5Currency::JPY->minorUnitDigits(); // 0
6Currency::from('EUR'); // Currency::EUR

Larasell supports all currencies that Stripe supports (139 in total):

  • AED — United Arab Emirates Dirham
  • AFN — Afghan Afghani
  • ALL — Albanian Lek
  • AMD — Armenian Dram
  • ANG — Netherlands Antillean Guilder
  • AOA — Angolan Kwanza
  • ARS — Argentine Peso
  • AUD — Australian Dollar
  • AWG — Aruban Florin
  • AZN — Azerbaijani Manat
  • BAM — Bosnia and Herzegovina Convertible Mark
  • BBD — Barbadian Dollar
  • BDT — Bangladeshi Taka
  • BHD — Bahraini Dinar
  • BIF — Burundian Franc
  • BMD — Bermudian Dollar
  • BND — Brunei Dollar
  • BOB — Bolivian Boliviano
  • BRL — Brazilian Real
  • BSD — Bahamian Dollar
  • BWP — Botswana Pula
  • BYN — Belarusian Ruble
  • BZD — Belize Dollar
  • CAD — Canadian Dollar
  • CDF — Congolese Franc
  • CHF — Swiss Franc
  • CLP — Chilean Peso
  • CNY — Chinese Renminbi Yuan
  • COP — Colombian Peso
  • CRC — Costa Rican Colón
  • CVE — Cape Verdean Escudo
  • CZK — Czech Koruna
  • DJF — Djiboutian Franc
  • DKK — Danish Krone
  • DOP — Dominican Peso
  • DZD — Algerian Dinar
  • EGP — Egyptian Pound
  • ETB — Ethiopian Birr
  • EUR — Euro
  • FJD — Fijian Dollar
  • FKP — Falkland Islands Pound
  • GBP — British Pound
  • GEL — Georgian Lari
  • GIP — Gibraltar Pound
  • GMD — Gambian Dalasi
  • GNF — Guinean Franc
  • GTQ — Guatemalan Quetzal
  • GYD — Guyanese Dollar
  • HKD — Hong Kong Dollar
  • HNL — Honduran Lempira
  • HTG — Haitian Gourde
  • HUF — Hungarian Forint
  • IDR — Indonesian Rupiah
  • ILS — Israeli New Shekel
  • INR — Indian Rupee
  • ISK — Icelandic Króna
  • JMD — Jamaican Dollar
  • JOD — Jordanian Dinar
  • JPY — Japanese Yen
  • KES — Kenyan Shilling
  • KGS — Kyrgyzstani Som
  • KHR — Cambodian Riel
  • KMF — Comorian Franc
  • KRW — South Korean Won
  • KWD — Kuwaiti Dinar
  • KYD — Cayman Islands Dollar
  • KZT — Kazakhstani Tenge
  • LAK — Lao Kip
  • LBP — Lebanese Pound
  • LKR — Sri Lankan Rupee
  • LRD — Liberian Dollar
  • LSL — Lesotho Loti
  • MAD — Moroccan Dirham
  • MDL — Moldovan Leu
  • MGA — Malagasy Ariary
  • MKD — Macedonian Denar
  • MMK — Myanmar Kyat
  • MNT — Mongolian Tögrög
  • MOP — Macanese Pataca
  • MUR — Mauritian Rupee
  • MVR — Maldivian Rufiyaa
  • MWK — Malawian Kwacha
  • MXN — Mexican Peso
  • MYR — Malaysian Ringgit
  • MZN — Mozambican Metical
  • NAD — Namibian Dollar
  • NGN — Nigerian Naira
  • NIO — Nicaraguan Córdoba
  • NOK — Norwegian Krone
  • NPR — Nepalese Rupee
  • NZD — New Zealand Dollar
  • OMR — Omani Rial
  • PAB — Panamanian Balboa
  • PEN — Peruvian Sol
  • PGK — Papua New Guinean Kina
  • PHP — Philippine Peso
  • PKR — Pakistani Rupee
  • PLN — Polish Złoty
  • PYG — Paraguayan Guaraní
  • QAR — Qatari Riyal
  • RON — Romanian Leu
  • RSD — Serbian Dinar
  • RUB — Russian Ruble
  • RWF — Rwandan Franc
  • SAR — Saudi Riyal
  • SBD — Solomon Islands Dollar
  • SCR — Seychellois Rupee
  • SEK — Swedish Krona
  • SGD — Singapore Dollar
  • SHP — Saint Helena Pound
  • SLE — Sierra Leonean Leone
  • SOS — Somali Shilling
  • SRD — Surinamese Dollar
  • STD — São Tomé and Príncipe Dobra
  • SZL — Swazi Lilangeni
  • THB — Thai Baht
  • TJS — Tajikistani Somoni
  • TND — Tunisian Dinar
  • TOP — Tongan Paʻanga
  • TRY — Turkish Lira
  • TTD — Trinidad and Tobago Dollar
  • TWD — New Taiwan Dollar
  • TZS — Tanzanian Shilling
  • UAH — Ukrainian Hryvnia
  • UGX — Ugandan Shilling
  • USD — US Dollar
  • UYU — Uruguayan Peso
  • UZS — Uzbekistani Sum
  • VND — Vietnamese Đồng
  • VUV — Vanuatu Vatu
  • WST — Samoan Tala
  • XAF — Central African CFA Franc
  • XCD — East Caribbean Dollar
  • XCG — Caribbean Guilder
  • XOF — West African CFA Franc
  • XPF — CFP Franc
  • YER — Yemeni Rial
  • ZAR — South African Rand
  • ZMW — Zambian Kwacha

Note: Stripe still uses the outdated STD code for the São Tomé and Príncipe dobra (ISO 4217 renamed it to STN in 2018), so Larasell matches Stripe's code for compatibility.