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

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures image

Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures

Read article
Laravel Read Replicas: Check if a Replica Has Caught Up image

Laravel Read Replicas: Check if a Replica Has Caught Up

Read article
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article