douwei7203
2015-05-21 13:02
浏览 28
已采纳

从数字中获取价值

I have a number in this format:

3 320,17

And I need to get it like:

3320,17

if I use:

floatval($price)

it only return the number 3 separated by the space.

Any idea how to solve?

图片转代码服务由CSDN问答提供 功能建议

我有一个这种格式的数字:

  3 320  ,17 
   
 
 

我需要得到它:

  3320,17 
    
 
 

如果我使用:

  floatval($ price)
   
 
 

它只返回由空格分隔的数字3。

任何想法如何解决?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxin1980 2015-05-21 13:04
    已采纳

    This should work for you:

    Just replace the space and the comma in your number with str_replace()

    echo (float) str_replace([" ", ","], ["", "."], $str);
    

    output:

    3320.17
    

    To get your float number now in your expected format just use number_format().

    $str = "3 320,17";
    $floatNumber = (float)str_replace([" ", ","], ["", "."], $str);//Create valid float number
    echo number_format($floatNumber, 2, ",", "");
    
    点赞 打赏 评论

相关推荐 更多相似问题