dswqw66280 2017-08-30 03:59
浏览 39
已采纳

PHP变量在乘法后不显示0

Im stuck at this part of my code. So I am trying to get the 30% of a specific value but I am not getting the right output. please see value below.

foreach($salary->getValues() as $key => $value) {
    echo 'salary:       '.$value['salary'];
    echo 'gross salary: '.$value['salary'] * .30;
}

lets assume that the salary array that we are getting is 40,000 (I am already getting this in my code) now multiply it .30 the answer must be 12,000. Therefore

Expected Output:

salary: 40,000 gross salary: 12,000

What is happening

salary: 40,000 gross salary: 12

Help please

  • 写回答

4条回答 默认 最新

  • doulun1939 2017-08-30 04:36
    关注

    "40,000" is a string. When trying to multiply, PHP tries to convert it to an integer, encounters the comma (,) and returns an error (but, oddly, then seems to continue the process using the int it got to before the comma)

    One way would be to remove the comma before multiplying, then return it to number format after the multiplication.

    $newSalary = number_format(str_replace("," , "", $value['salary']) * .30);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。