Parsing Localized Numbers with Laravel's Number Class

Parsing Localized Numbers with Laravel's Number Class

Need to parse localized numbers in your Laravel application? The Number class now includes parsing methods that handle locale-specific number formats using PHP's Intl extension.

When working with international applications, you often receive number data in various locale-specific formats. Laravel's new parsing methods leverage PHP's NumberFormatter class to correctly interpret localized number strings, making it easier to handle numeric data from different regions.

Let's see how it works:

use Illuminate\Support\Number;

// Basic parsing
Number::parse($string);
Number::parseInt($string);
Number::parseFloat($string);

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

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

Real-World Example

Here's how you might use these methods in an international e-commerce system:

class LocalizedDataProcessor
{
    public function processOrderData(array $orderData, string $locale)
    {
        return [
            'total' => Number::parseFloat($orderData['total'], locale: $locale),
            'tax' => Number::parseFloat($orderData['tax'], locale: $locale),
            'quantity' => Number::parseInt($orderData['quantity'], locale: $locale),
            'weight' => Number::parse(
                string: $orderData['weight'],
                type: NumberFormatter::TYPE_DOUBLE,
                locale: $locale
            )
        ];
    }

    public function handleCsvImport(string $filePath, string $locale)
    {
        $data = [];
        
        foreach ($this->readCsv($filePath) as $row) {
            $data[] = [
                'price' => Number::parseFloat($row['price'], locale: $locale),
                'discount' => Number::parseFloat($row['discount'], locale: $locale),
                'stock' => Number::parseInt($row['stock'], locale: $locale)
            ];
        }
        
        return $data;
    }
}

// Usage examples
$processor = new LocalizedDataProcessor();

// German format: "1.234,56" becomes 1234.56
$germanData = $processor->processOrderData([
    'total' => '1.234,56',
    'tax' => '234,56',
    'quantity' => '10'
], 'de');

// French format: "1 234,56" becomes 1234.56
$frenchData = $processor->processOrderData([
    'total' => '1 234,56',
    'tax' => '234,56',
    'quantity' => '10'
], 'fr');

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

Stay Updated with More Laravel Tips

Enjoyed this article? There's plenty more where that came from! Subscribe to our channels to stay updated with the latest Laravel tips, tricks, and best practices:

Subscribe to Harris Raftopoulos

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe