PHP format string to numbers seperated by two decimal points

僤鯓⒐⒋嵵緔 提交于 2019-12-25 05:05:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!