PHP根据变量号删除尾随零

时间:2019-02-15 13:50:34

标签: php

基于某些选项,我想省略PHP中数字的结尾零:

decimals =通常要返回的重要小数位数

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

minDecimalsIfZero =当removeTrailingZero = true时使用

预期示例:

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

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

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;
}

2 个答案:

答案 0 :(得分:1)

如果我正确理解,这应该可以满足您的需求。请参阅评论:

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 :(得分:-1)

找到了可能的解决方案:

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;
}