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

t0berius's avatar

format input (number_format() )

User can input values like: -1.00 -1 -1,00

Now I need to return this values formated to the "comma" version (1->1,00 1.00->1,00). When I use:

number_format($request->amount, 2, ',','')

And use "1,00" as input I get this error:

A non well formed numeric value encountered

What's the best way to solve this, but being able to handle all three types of input though.

0 likes
3 replies
t0berius's avatar

@Snapey

I tried to use this:

    $value = str_replace ([',','.'], '' , $request->amount);

    dd(number_format($value, 2, ',',''));

Doesn't work too. When I input numbers like: 1.00 I get 100,00 at the end. I need to get an output like this:

input:|output:
1.00     1,00
1        1,00
1,51     1,51

So as you see all I need to get is the output seperated by two decimal points (,).

Snapey's avatar

replace comma with dot then do the number format

    $value = str_replace (',', '.' , $request->amount);

    dd(number_format($value, 2, ',',''));

Please or to participate in this conversation.