doucaigai7176 2014-11-24 12:55
浏览 16
已采纳

在PHP中将fraction转换为float

How to convert 1/4 to 0.25 in PHP.

In form I insert number as 1/4. In action page I get the post variable as 1/4. How can I convert this to 0.25. I think in action page this is obtained as string. Thats why it displayed as 1/4. But I need 0.25. How can I do this??

main page

<input type="text" name="a" id="a">

action page

$a = $_POST['a'];
echo $a;  //gives 1/4 but need 0.25

Please help..

  • 写回答

3条回答 默认 最新

  • dongwu9063 2014-11-24 12:58
    关注

    You can use a function like this:

    <?php
    
        function calculate_string( $mathString )    {
            $mathString = trim($mathString);
            $mathString = str_replace ('[^0-9\+-\*\/\(\) ]', '', $mathString); 
    
            $compute = create_function("", "return (" . $mathString . ");" );
            return 0 + $compute();
        }
    
        echo calculate_string("1/4");
    
    
    ?>
    

    Output:

    0.25
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题