Laravel Tutorials

Parse Localized Numbers with Laravel's Number Class

Published
Parse Localized Numbers with Laravel's Number Class image

Laravel's Number class introduces parsing methods that handle locale-specific number formats using PHP's Intl extension, making international numeric data processing seamless.

Handle different locale formats automatically:

use Illuminate\Support\Number;

Number::parse($string);
Number::parseInt($string);
Number::parseFloat($string);

// With specific locale
Number::parseFloat(string: $string, locale: 'fr');

// With custom type
Number::parse(
    string: $string,
    type: NumberFormatter::TYPE_INT64,
    locale: 'fr',
);

Here's how you might use these methods in a financial reporting system:

class FinancialDataProcessor
{
    public function processReportData(array $reportData, string $locale)
    {
        return [
            'revenue' => Number::parseFloat($reportData['revenue'], locale: $locale),
            'expenses' => Number::parseFloat($reportData['expenses'], locale: $locale),
            'transactions' => Number::parseInt($reportData['transactions'], locale: $locale),
            'margin' => Number::parse(
                string: $reportData['margin'],
                type: NumberFormatter::TYPE_DOUBLE,
                locale: $locale
            )
        ];
    }

    public function importFinancialData(string $filePath, string $locale)
    {
        $records = [];
        
        foreach ($this->readSpreadsheet($filePath) as $row) {
            $records[] = [
                'amount' => Number::parseFloat($row['amount'], locale: $locale),
                'commission' => Number::parseFloat($row['commission'], locale: $locale),
                'units' => Number::parseInt($row['units'], locale: $locale)
            ];
        }
        
        return $records;
    }
}

$processor = new FinancialDataProcessor();

// Italian format: "1.234,56" becomes 1234.56
$italianData = $processor->processReportData([
    'revenue' => '1.234,56',
    'expenses' => '456,78',
    'transactions' => '25'
], 'it');

// Spanish format: "1.234,56" becomes 1234.56
$spanishData = $processor->processReportData([
    'revenue' => '1.234,56',
    'expenses' => '456,78',
    'transactions' => '25'
], 'es');

These parsing methods handle the complexity of different number formats automatically, making your international applications more robust and user-friendly.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Load Eloquent Models From Form Request Input in Laravel image

Load Eloquent Models From Form Request Input in Laravel

Read article
Laravel FFmpeg 9: Encode Video and Stream HLS and DASH image

Laravel FFmpeg 9: Encode Video and Stream HLS and DASH

Read article
Migration Durations and Job Memory Usage in Laravel 13.45 image

Migration Durations and Job Memory Usage in Laravel 13.45

Read article
Laravel CLI: One Binary for New PHP and Rust Laravel Apps image

Laravel CLI: One Binary for New PHP and Rust Laravel Apps

Read article
Phactorial: Render LaTeX Math to SVG and PNG in PHP image

Phactorial: Render LaTeX Math to SVG and PNG in PHP

Read article
Laravel, reimagined image

Laravel, reimagined

Read article