PHP code example of laxmidhar / desi-currency

1. Go to this page and download the library: Download laxmidhar/desi-currency library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laxmidhar / desi-currency example snippets


use Laxmidhar\DesiCurrency\Facades\Currency;

// Format in Indian style
Currency::format(123456.78);
// Output: "₹1,23,456.78"

// Convert to words
Currency::toWords(150000);
// Output: "₹1.5 Lakh"

// Shorthand notation
Currency::toShorthand(2500000);
// Output: "₹25L"

// Parse back to number
Currency::parse('1.5L');
// Output: 150000

use Laxmidhar\DesiCurrency\Support\CurrencyService;

CurrencyService::format(123456.78);
CurrencyService::toWords(150000);
CurrencyService::toShorthand(2500000);

use Laxmidhar\DesiCurrency\Support\CurrencyService;

class InvoiceController extends Controller
{
    public function __construct(protected CurrencyService $currency) {}

    public function show($id)
    {
        $invoice = Invoice::find($id);
        $formatted = $this->currency->format($invoice->total);
        $inWords = $this->currency->toIndianWords($invoice->total);

        return view('invoice.show', compact('invoice', 'formatted', 'inWords'));
    }
}

Currency::format(1234.56);
// Output: "₹1,234.56"

Currency::format(123456.78);
// Output: "₹1,23,456.78"

Currency::format(12345678.90);
// Output: "₹1,23,45,678.90"

Currency::format(1234.56, false);
// Output: "1,234.56"

Currency::format(-5000);
// Output: "-₹5,000.00"

Currency::formatWhole(123456.78);
// Output: "₹1,23,457"

Currency::formatWhole(1234.50);
// Output: "₹1,235"

Currency::formatWhole(1000000);
// Output: "₹10,00,000"

Currency::formatWhole(1234.56, false);
// Output: "1,235"

Currency::formatAccounting(1000);
// Output: "₹1,000.00"

Currency::formatAccounting(-1000);
// Output: "(₹1,000.00)"

Currency::formatAccounting(-25000.50);
// Output: "(₹25,000.50)"

Currency::toWords(500);
// Output: "₹500"

Currency::toWords(5000);
// Output: "₹5K"

Currency::toWords(50000);
// Output: "₹50K"

Currency::toWords(150000);
// Output: "₹1.5 Lakh"

Currency::toWords(2500000);
// Output: "₹25 Lakh"

Currency::toWords(15000000);
// Output: "₹1.5 Crore"

Currency::toWords(250000000);
// Output: "₹25 Crore"

Currency::toWords(150000, false);
// Output: "1.5 Lakh"

Currency::toShorthand(500);
// Output: "₹500"

Currency::toShorthand(5000);
// Output: "₹5K"

Currency::toShorthand(150000);
// Output: "₹1.5L"

Currency::toShorthand(2500000);
// Output: "₹25L"

Currency::toShorthand(15000000);
// Output: "₹1.5Cr"

Currency::toShorthand(250000000);
// Output: "₹25Cr"

Currency::toShorthand(-150000);
// Output: "-₹1.5L"

Currency::toLakhs(50000);
// Output: "₹0.50 Lakhs"

Currency::toLakhs(150000);
// Output: "₹1.50 Lakhs"

Currency::toLakhs(2500000);
// Output: "₹25.00 Lakhs"

Currency::toLakhs(15000000);
// Output: "₹150.00 Lakhs"

Currency::toLakhs(150000, 1);
// Output: "₹1.5 Lakhs"

Currency::toLakhs(100000);
// Output: "₹1.00 Lakh"

Currency::toLakhs(150000, 2, false);
// Output: "1.50 Lakhs"

Currency::toCrores(5000000);
// Output: "₹0.50 Crores"

Currency::toCrores(15000000);
// Output: "₹1.50 Crores"

Currency::toCrores(250000000);
// Output: "₹25.00 Crores"

Currency::toCrores(15000000, 1);
// Output: "₹1.5 Crores"

Currency::toCrores(10000000);
// Output: "₹1.00 Crore"

Currency::toCrores(15000000, 2, false);
// Output: "1.50 Crores"

Currency::toIndianWords(123);
// Output: "One Hundred Twenty Three Rupees"

Currency::toIndianWords(1234.50);
// Output: "One Thousand Two Hundred Thirty Four Rupees and Fifty Paise"

Currency::toIndianWords(123456);
// Output: "One Lakh Twenty Three Thousand Four Hundred Fifty Six Rupees"

Currency::toIndianWords(12345678);
// Output: "One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight Rupees"

Currency::toIndianWords(100.25);
// Output: "One Hundred Rupees and Twenty Five Paise"

Currency::toIndianWords(-500);
// Output: "Negative Five Hundred Rupees"

Currency::parse('1L');
// Output: 100000

Currency::parse('1.5L');
// Output: 150000

Currency::parse('2.5Cr');
// Output: 25000000

Currency::parse('500K');
// Output: 500000

Currency::parse('1 Lakh');
// Output: 100000

Currency::parse('2.5 Crore');
// Output: 25000000

Currency::parse('₹1.5L');
// Output: 150000

Currency::parse('Rs 2.5Cr');
// Output: 25000000

Currency::parse('-1.5L');
// Output: -150000

Currency::parse('1,50,000');
// Output: 150000

Currency::symbol();
// Output: "₹"

// Usage in views
echo Currency::symbol() . '1,000';
// Output: "₹1,000"

Currency::splitRupeesPaise(1234.56);
// Output: ['rupees' => 1234, 'paise' => 56]

Currency::splitRupeesPaise(100.25);
// Output: ['rupees' => 100, 'paise' => 25]

Currency::splitRupeesPaise(500);
// Output: ['rupees' => 500, 'paise' => 0]

Currency::splitRupeesPaise(-1234.56);
// Output: ['rupees' => -1234, 'paise' => 56]

// Usage example
$split = Currency::splitRupeesPaise(1234.56);
echo "Rupees: {$split['rupees']}, Paise: {$split['paise']}";
// Output: "Rupees: 1234, Paise: 56"

Currency::formatWithSuffix(1000, 'per month');
// Output: "₹1,000.00 per month"

Currency::formatWithSuffix(50000, 'annual');
// Output: "₹50,000.00 annual"

Currency::formatWithSuffix(25000, 'onwards');
// Output: "₹25,000.00 onwards"

Currency::formatWithSuffix(100000, 'only');
// Output: "₹1,00,000.00 only"

Currency::formatWithSuffix(1000);
// Output: "₹1,000.00"

Currency::isLakhsRange(50000);
// Output: false (less than 1 Lakh)

Currency::isLakhsRange(150000);
// Output: true (1.5 Lakhs)

Currency::isLakhsRange(2500000);
// Output: true (25 Lakhs)

Currency::isLakhsRange(15000000);
// Output: false (1.5 Crores)

// Usage example
$amount = 250000;
if (Currency::isLakhsRange($amount)) {
    echo Currency::toLakhs($amount);
} else {
    echo Currency::format($amount);
}

Currency::isCroresRange(5000000);
// Output: false (50 Lakhs)

Currency::isCroresRange(10000000);
// Output: true (1 Crore)

Currency::isCroresRange(250000000);
// Output: true (25 Crores)

// Usage example
$amount = 15000000;
if (Currency::isCroresRange($amount)) {
    echo Currency::toCrores($amount);
} else {
    echo Currency::toLakhs($amount);
}

use Laxmidhar\DesiCurrency\Facades\Currency;

// Product price formatting
$product = Product::find(1);
$price = $product->price; // 149900

// For display
echo Currency::toShorthand($price);
// Output: "₹1.5L"

// For detailed view
echo Currency::format($price);
// Output: "₹1,49,900.00"

// For cards/widgets
echo Currency::toWords($price);
// Output: "₹1.5 Lakh"

use Laxmidhar\DesiCurrency\Facades\Currency;

$invoice = Invoice::find(1);
$total = $invoice->total; // 125750.50

// Invoice total
echo Currency::format($total);
// Output: "₹1,25,750.50"

// Amount in words
echo Currency::toIndianWords($total);
// Output: "One Lakh Twenty Five Thousand Seven Hundred Fifty Rupees and Fifty Paise"

// Split for detailed view
$split = Currency::splitRupeesPaise($total);
echo "Rupees: {$split['rupees']}, Paise: {$split['paise']}";
// Output: "Rupees: 125750, Paise: 50"

use Laxmidhar\DesiCurrency\Facades\Currency;

// Annual revenue
$revenue = 125000000; // 12.5 Crores

echo Currency::toShorthand($revenue);
// Output: "₹12.5Cr"

// Monthly revenue
$monthly = $revenue / 12;

if (Currency::isCroresRange($monthly)) {
    echo Currency::toCrores($monthly);
} else {
    echo Currency::toLakhs($monthly);
}
// Output: "₹1.04 Crores"

use Laxmidhar\DesiCurrency\Facades\Currency;

$salary = 1800000; // 18 Lakhs per annum

// Annual
echo Currency::toLakhs($salary) . ' per annum';
// Output: "₹18.00 Lakhs per annum"

// Monthly
$monthly = $salary / 12;
echo Currency::format($monthly) . ' per month';
// Output: "₹1,50,000.00 per month"

use Laxmidhar\DesiCurrency\Facades\Currency;

// User enters "2.5L" in a form
$userInput = request('budget'); // "2.5L"

// Parse to numeric value
$numericValue = Currency::parse($userInput);
// Output: 250000

// Store in database
$property->budget = $numericValue;
$property->save();

// Display back to user
echo Currency::toWords($property->budget);
// Output: "₹2.5 Lakh"

use Laxmidhar\DesiCurrency\Facades\Currency;

$profit = 5500000;
$loss = -1200000;

// Profit
echo Currency::formatAccounting($profit);
// Output: "₹55,00,000.00"

// Loss
echo Currency::formatAccounting($loss);
// Output: "(₹12,00,000.00)"

// Summary
echo Currency::toShorthand($profit);
// Output: "₹55L"

use Laxmidhar\DesiCurrency\Facades\Currency;

$properties = Property::all();

foreach ($properties as $property) {
    $price = $property->price;

    // Display appropriate format based on range
    if (Currency::isCroresRange($price)) {
        echo $property->title . ': ' . Currency::toCrores($price);
    } else if (Currency::isLakhsRange($price)) {
        echo $property->title . ': ' . Currency::toLakhs($price);
    } else {
        echo $property->title . ': ' . Currency::format($price);
    }
}

// Output examples:
// "Luxury Villa: ₹2.50 Crores"
// "2BHK Apartment: ₹45.00 Lakhs"
// "Plot: ₹25,00,000.00"

// Good: Consistent format
echo Currency::toShorthand($amount); // Throughout app

// Avoid: Mixing formats randomly
echo Currency::format($amount);
echo Currency::toWords($amount);

// Storage (numeric)
$product->price = 150000;

// Display (formatted)
echo Currency::toWords($product->price);

$input = request('amount'); // "1.5L"
$numeric = Currency::parse($input);
$model->amount = $numeric; // Store: 150000

// Dashboards: Shorthand
echo Currency::toShorthand($revenue);

// Reports: Full format
echo Currency::format($revenue);

// Invoices: Words
echo Currency::toIndianWords($total);

// Cards: Words format
echo Currency::toWords($amount);

$balance = -5000;

// Use accounting format for negatives
echo Currency::formatAccounting($balance);
// Output: "(₹5,000.00)"

use Laxmidhar\DesiCurrency\Facades\Currency;

test('formats amount in Indian style', function () {
    expect(Currency::format(123456.78))
        ->toBe('₹1,23,456.78');
});

test('converts to lakhs notation', function () {
    expect(Currency::toWords(150000))
        ->toBe('₹1.5 Lakh');
});

test('parses shorthand notation', function () {
    expect(Currency::parse('1.5L'))
        ->toBe(150000.0);
});

test('converts to Indian words', function () {
    expect(Currency::toIndianWords(1234.50))
        ->toBe('One Thousand Two Hundred Thirty Four Rupees and Fifty Paise');
});