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 Image Responses: Serve Resized Images From Routes image

Laravel Image Responses: Serve Resized Images From Routes

Read article
Pause All Laravel Queues During a Deploy image

Pause All Laravel Queues During a Deploy

Read article
Laravel Terminal UI for the artisan dev Command image

Laravel Terminal UI for the artisan dev Command

Read article
Pause All Queues and a New artisan dev UI in Laravel 13.25 image

Pause All Queues and a New artisan dev UI in Laravel 13.25

Read article
Laravel monitoring that doesn't bill you by your traffic image

Laravel monitoring that doesn't bill you by your traffic

Read article
Mock PHP Classes in Tests With the Double Library image

Mock PHP Classes in Tests With the Double Library

Read article