Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vamoose's avatar
Level 12

Livewire Input Field Decimal Separator Issue

I have a laravel 11 app using livewire and flux UI. I have a public variable called $amounts, which is an array with a key of $account_id, and a value of the amount.

ie:

$amounts[$account_id] = 50.35;

When printing out on the page, it prints 50.35, but it loads into the input field with a type of number as 50,35

I have tried setting locale, reformatting the array before the page renders, number_format when setting the value, using javascript to try overwrite the value - but that feels like I am going way beyond what should be convention to get this working. I am using flux:

<flux:input type="text" wire:model.live.debounce.250ms="amounts.{{ $account_id }}" step="0.1" lang="en" type="number" id="amount-{{ $account_id }}" />

If the user enters 50.35, then it shows correctly, but if I update it programatically and the view rerenders, then it shows as 50,35 again instead of 50.35.

What am I doing wrong? What should I be doing to enforce that it uses . as the default decimal separator?

0 likes
4 replies
LaryAI's avatar
Level 58

The issue you're experiencing with the decimal separator is likely related to the locale settings of your application or the browser. When dealing with numbers in input fields, especially with type number, the browser might automatically format the number based on the user's locale settings, which can lead to the use of a comma instead of a period as the decimal separator.

Here are a few steps you can take to ensure that the decimal separator remains consistent:

  1. Ensure Locale Consistency: Make sure that your application is set to use a consistent locale that uses a period as the decimal separator. You can set the locale in your Laravel application by configuring it in the config/app.php file:

    'locale' => 'en',
    
  2. Use type="text" Instead of type="number": Since the type="number" input can be affected by the browser's locale settings, consider using type="text" and manually handling the validation and formatting. This gives you more control over how the input is displayed and processed.

  3. JavaScript Formatting: If you need to ensure the format on the client side, you can use JavaScript to format the number before it is displayed in the input field. Here's a simple example using JavaScript:

    document.addEventListener('livewire:load', function () {
        Livewire.hook('message.processed', (message, component) => {
            document.querySelectorAll('input[type="text"]').forEach(input => {
                if (input.value.includes(',')) {
                    input.value = input.value.replace(',', '.');
                }
            });
        });
    });
    
  4. Livewire Component Method: You can also format the number in your Livewire component before it is sent to the view. For example:

    public function mount()
    {
        foreach ($this->amounts as $accountId => $amount) {
            $this->amounts[$accountId] = number_format($amount, 2, '.', '');
        }
    }
    
  5. HTML lang Attribute: Ensure that the lang attribute is set correctly in your HTML. Although you have already set lang="en", double-check that it is applied correctly to the parent elements as well.

By following these steps, you should be able to maintain a consistent decimal separator in your input fields. If the issue persists, consider checking the browser's language settings or any other scripts that might be affecting the input field's value.

Snapey's avatar

its a strange one because the locale of a number field is set by the user's browser, so this implies your computer has the locale of a country that uses a comma as its decimal seperator

vamoose's avatar
Level 12

@Snapey But surely there should be a way to override it and force using a decimal instead of a comma?

Please or to participate in this conversation.