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 allowedUse 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 InvalidArgumentExceptionLarasell 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 trueHere'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 trueIf 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 0Note: 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 trueFormatting 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::EURLarasell supports all currencies that Stripe supports (139 in total):
AED— United Arab Emirates DirhamAFN— Afghan AfghaniALL— Albanian LekAMD— Armenian DramANG— Netherlands Antillean GuilderAOA— Angolan KwanzaARS— Argentine PesoAUD— Australian DollarAWG— Aruban FlorinAZN— Azerbaijani ManatBAM— Bosnia and Herzegovina Convertible MarkBBD— Barbadian DollarBDT— Bangladeshi TakaBHD— Bahraini DinarBIF— Burundian FrancBMD— Bermudian DollarBND— Brunei DollarBOB— Bolivian BolivianoBRL— Brazilian RealBSD— Bahamian DollarBWP— Botswana PulaBYN— Belarusian RubleBZD— Belize DollarCAD— Canadian DollarCDF— Congolese FrancCHF— Swiss FrancCLP— Chilean PesoCNY— Chinese Renminbi YuanCOP— Colombian PesoCRC— Costa Rican ColónCVE— Cape Verdean EscudoCZK— Czech KorunaDJF— Djiboutian FrancDKK— Danish KroneDOP— Dominican PesoDZD— Algerian DinarEGP— Egyptian PoundETB— Ethiopian BirrEUR— EuroFJD— Fijian DollarFKP— Falkland Islands PoundGBP— British PoundGEL— Georgian LariGIP— Gibraltar PoundGMD— Gambian DalasiGNF— Guinean FrancGTQ— Guatemalan QuetzalGYD— Guyanese DollarHKD— Hong Kong DollarHNL— Honduran LempiraHTG— Haitian GourdeHUF— Hungarian ForintIDR— Indonesian RupiahILS— Israeli New ShekelINR— Indian RupeeISK— Icelandic KrónaJMD— Jamaican DollarJOD— Jordanian DinarJPY— Japanese YenKES— Kenyan ShillingKGS— Kyrgyzstani SomKHR— Cambodian RielKMF— Comorian FrancKRW— South Korean WonKWD— Kuwaiti DinarKYD— Cayman Islands DollarKZT— Kazakhstani TengeLAK— Lao KipLBP— Lebanese PoundLKR— Sri Lankan RupeeLRD— Liberian DollarLSL— Lesotho LotiMAD— Moroccan DirhamMDL— Moldovan LeuMGA— Malagasy AriaryMKD— Macedonian DenarMMK— Myanmar KyatMNT— Mongolian TögrögMOP— Macanese PatacaMUR— Mauritian RupeeMVR— Maldivian RufiyaaMWK— Malawian KwachaMXN— Mexican PesoMYR— Malaysian RinggitMZN— Mozambican MeticalNAD— Namibian DollarNGN— Nigerian NairaNIO— Nicaraguan CórdobaNOK— Norwegian KroneNPR— Nepalese RupeeNZD— New Zealand DollarOMR— Omani RialPAB— Panamanian BalboaPEN— Peruvian SolPGK— Papua New Guinean KinaPHP— Philippine PesoPKR— Pakistani RupeePLN— Polish ZłotyPYG— Paraguayan GuaraníQAR— Qatari RiyalRON— Romanian LeuRSD— Serbian DinarRUB— Russian RubleRWF— Rwandan FrancSAR— Saudi RiyalSBD— Solomon Islands DollarSCR— Seychellois RupeeSEK— Swedish KronaSGD— Singapore DollarSHP— Saint Helena PoundSLE— Sierra Leonean LeoneSOS— Somali ShillingSRD— Surinamese DollarSTD— São Tomé and Príncipe DobraSZL— Swazi LilangeniTHB— Thai BahtTJS— Tajikistani SomoniTND— Tunisian DinarTOP— Tongan PaʻangaTRY— Turkish LiraTTD— Trinidad and Tobago DollarTWD— New Taiwan DollarTZS— Tanzanian ShillingUAH— Ukrainian HryvniaUGX— Ugandan ShillingUSD— US DollarUYU— Uruguayan PesoUZS— Uzbekistani SumVND— Vietnamese ĐồngVUV— Vanuatu VatuWST— Samoan TalaXAF— Central African CFA FrancXCD— East Caribbean DollarXCG— Caribbean GuilderXOF— West African CFA FrancXPF— CFP FrancYER— Yemeni RialZAR— South African RandZMW— 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.