duanqiao1880 2015-06-10 09:39
浏览 21
已采纳

如果逗号后面没有数字,则将货币转换为单个数字

I have a price, let's say $price = 9,00 €. If price have 2 zeroes after comma is there easy way to transform it to 9 € ? I mean is there a function for that or something. If that is not possible with character, how to do that only with integers 9,00 > 9, but 9,20 would stay 9,20.

I know it could be done with substr or some other ways, but I'd like to know if PHP have function for this situation.

  • 写回答

1条回答 默认 最新

  • doubu1970 2015-06-10 09:45
    关注

    You have to use NumberFormatter.

    $formatter = new NumberFormatter('de-DE', NumberFormatter::CURRENCY);
    

    parse it to float

    $value = $formatter->parseCurrency('9,00 €', 'EUR');
    

    check if is int or float

    $digits = floor($value) == $value ? 0 : 2;
    

    reformat

    $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $digits);
    echo $formatter->formatCurrency($value, 'EUR');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改