首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP基于变量数去除尾随零

PHP基于变量数去除尾随零
EN

Stack Overflow用户
提问于 2019-02-15 13:50:33
回答 2查看 103关注 0票数 0

我想根据以下几个选项,从PHP中的数字中省略尾数为零:

decimals =作为规则返回的重要十进制数

removeTrailingZero = true / false:删除或不拖尾零

minDecimalsIfZero =当removeTrailingZero = true时使用

预期的例子:

代码语言:javascript
复制
echo get_formatted_number(123.5670, array("decimals" => 2));
// returned: 123.57

echo get_formatted_number(123, array("decimals" => 2));
// returned: 123.00

echo get_formatted_number(123, array("decimals" => 8, "removeTrailingZero" => false));
// returned: 123.00000000

echo get_formatted_number(123, array("decimals" => 8, "removeTrailingZero" => true));
// returned: 123

echo get_formatted_number(123, array("decimals" => 8, "removeTrailingZero" => true, "minDecimalsIfZero" => 2));
// returned: 123.00

echo get_formatted_number(123.4657, array("decimals" => 8, "removeTrailingZero" => true, "minDecimalsIfZero" => 2));
// returned: 123.4657 <-- in this case i'd like to return this cause i've removed the trailing zero and number of decimals (4) is bigger than minDecimalsIfZero (2): ignore decimals option (8)

echo get_formatted_number(123.4657, array("decimals" => 8, "removeTrailingZero" => false, "minDecimalsIfZero" => 2));
// returned: 123.46570000 <-- in this case i'd like to return this cause removeTrailingZero is false

这是我要修改的实际功能。

代码语言:javascript
复制
function get_formatted_number($number, $options = array()) {

    if ( $number === "" ) {
        return "";
    }

    if ( $number === NULL ) {
        return "";
    }

    if ( isset($options['decimals']) ) {
        $decimals = (int)$options['decimals'] != "" ? (int)$options['decimals'] : 2;
    } else {
        $decimals = 2; //default = 2
    }

    $value = number_format($number, $decimals);

    return $value;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-15 14:38:30

如果我理解正确的话,这应该能满足你的需要。请参阅以下评论:

代码语言:javascript
复制
function get_formatted_number($number, array $options = []): string
{
  // Default values
  static $DEFAULT_DECIMALS = 2;
  static $DEFAULT_REMOVE_TRAILING_ZEROS = false;
  static $DEFAULT_TRAILING_ZEROS_DECIMALS = 0;

  // Retrieve/sanitize options
  $decimals = array_key_exists('decimals', $options) 
    ? max($DEFAULT_DECIMALS, (int)$options['decimals']) 
    : $DEFAULT_DECIMALS;
  $remove_trailing_zeros = array_key_exists('removeTrailingZero', $options) 
    ? !!$options['removeTrailingZero'] 
    : $DEFAULT_REMOVE_TRAILING_ZEROS;
  $trailing_zeros_decimals = array_key_exists('minDecimalsIfZero', $options) 
    ? max($DEFAULT_TRAILING_ZEROS_DECIMALS, (int)$options['minDecimalsIfZero']) 
    : $DEFAULT_TRAILING_ZEROS_DECIMALS;

  // Set number of decimals from options
  $formatted_number = number_format($number, $decimals);

  // If option is set, remove trailing zeros, keeping an optional minimum
  if ($remove_trailing_zeros) {
    $formatted_number = (float)$formatted_number;
    if ($trailing_zeros_decimals >= strlen(substr(strrchr($formatted_number, '.'), 1))) {
      $formatted_number = number_format($formatted_number, $trailing_zeros_decimals);
    }
  }

  return $formatted_number;
}

演示这里: https://3v4l.org/7eqBe

票数 1
EN

Stack Overflow用户

发布于 2019-02-15 14:08:13

找到了一个可能的解决办法:

代码语言:javascript
复制
function get_formatted_number($number, $options = array()) {

    if ( $number === "" ) {
        return "";
    }

    if ( $number === NULL ) {
        return "";
    }

    if ( isset($opzioni['removeTrailingZero']) ) {
        $removeTrailingZero= (bool)$opzioni['removeTrailingZero'];
    } else {
        $removeTrailingZero= false;
    }

    if ( isset($options['decimals']) ) {
        $decimals = (int)$options['decimals'] != "" ? (int)$options['decimals'] : 2;
    } else {
        $decimals = 2; //default = 2
    }

    if ( isset($opzioni['minDecimalsIfZero']) ) {
        $minDecimalsIfZero= $opzioni['minDecimalsIfZero'];
    } else {
        $minDecimalsIfZero = 2; //default
    }

    $value = number_format($number, $decimals);

    if ( $removeTrailingZero) {

        $value = preg_replace('/0{0,'.($decimals-$minDecimalsIfZero).'}$/', '', $value);

    } 

    return $value;
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54710773

复制
相关文章

相似问题

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