首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在哪里添加int $decimals = 0?

在哪里添加int $decimals = 0?
EN

Stack Overflow用户
提问于 2015-03-31 17:30:07
回答 1查看 37关注 0票数 0

我需要打印一个有两个小数点的数字,我相信我使用了这段代码int $decimals = 0,但我不知道我需要把它加到哪里。

下面是我的代码:

代码语言:javascript
复制
<?php 
  $tempPrice = str_replace(',',"", $price); //gets rid of "," 
  $tempPrice = substr($tempPrice,2); //removes currency from the front
  $tempPrice = floatval($tempPrice); //converts to double from string

  if($tempPrice > 1000) 
  {
    echo '£' . round(($tempPrice*0.038), 2) . ' per month';
  }
  else 
  {
    echo 'Lease to buy price not available on this product';
  }
 ?>

谢谢

EN

回答 1

Stack Overflow用户

发布于 2015-03-31 17:37:17

您可以使用money_format() php函数。http://php.net/money_format

例如在您的情况下

代码语言:javascript
复制
<?php 
  $tempPrice = str_replace(',',"", $price); //gets rid of "," 
  $tempPrice = substr($tempPrice,2); //removes currency from the front
  $tempPrice = floatval($tempPrice); //converts to double from string
  // set international format for the en_GB locale
  setlocale(LC_MONETARY, 'en_GB');    
  if($tempPrice > 1000) 
  {
  echo money_format('%i', $tempPrice ) . " per month";// output->"GBP 1,234.56 per month"
  }
  else 
  {
    echo 'Lease to buy price not available on this product';
  }
 ?>

此外,您还可以在$tempPrice上使用php number_format() http://php.net/number_format

代码语言:javascript
复制
echo "£ ". number_format($tempPrice ) . " per month";

默认情况下,使用number_format()英语表示法。您可以设置自己的小数点数、小数点设置符和千位分隔符

代码语言:javascript
复制
echo "£ ". number_format($tempPrice, 2, ".", "," ) . " per month"; // for $tempPrice=1203.52 output will be "£ 1,203.56 per month"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29365201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档