PHP 将字符串格式化为以两位小数分隔的数字

PHP format string to numbers seperated by two decimal points

用户可以按以下格式输入值:

现在我需要 return 使用两个小数点 (,) 格式化此值。当我使用:

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

并使用“1,00”作为输入我得到这个错误:

Notice: A non well formed numeric value encountered

解决这个问题并且仍然能够处理所有三种类型的输入的最佳方法是什么?

这是一个简短的例子:

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

试试这个:

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

以下应该有效:

$request->amount = str_replace(",", ".", $request->amount);
number_format($request->amount, 2, ',','');