简体   繁体   中英

PHP remove trailing zero based on variable number

I'd like to omit trailing zero from a number in PHP based on some options:

decimals = number of important decimal to return as a rule

removeTrailingZero = true / false: remove or not trailing zeros

minDecimalsIfZero = used when removeTrailingZero = true

Examples expected:

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

This is my actual function to modify.

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

If I understood correctly, this should do what you need. Refer to the comments:

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

Demo here: https://3v4l.org/7eqBe

Found a possible solution:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM