将PHP格式的字符串转换为由两位小数分隔的数字 [英] PHP format string to numbers seperated by two decimal points

查看:149
本文介绍了将PHP格式的字符串转换为由两位小数分隔的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  • 1.00

  • 1





  • 现在我需要使用两个小数点(,)来返回这个值。当我使用:

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



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

     注意:遇到的格式不正确的数值

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



    下面是一个简单的例子:

     用户输入:| output:
    1.00 1,00
    1 1,00
    1,51 1,51



      $ request-> amount = str_replace(h2_lin>解决方案

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


    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
    

    解决方案

    The following should work:

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

    这篇关于将PHP格式的字符串转换为由两位小数分隔的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆