问题
User can input values in following format:
- 1.00
- 1
- 1,00
Now I need to return this values formated by using two decimal points (,). When I use:
number_format($request->amount, 2, ',','')
And use "1,00" as input I get this error:
Notice: A non well formed numeric value encountered
What's the best way to solve this and still be able to handle all three types of input?
Here's a short example:
input of user:|output:
1.00 1,00
1 1,00
1,51 1,51
回答1:
The following should work:
$request->amount = str_replace(",", ".", $request->amount);
number_format($request->amount, 2, ',','');
回答2:
Try this :
number_format((int)$request->amount, 2, ',','');
来源:https://stackoverflow.com/questions/35923007/php-format-string-to-numbers-seperated-by-two-decimal-points