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

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article