You are here

public static function Calculator::trim in Physical Fields 8

Trims the given number.

By default bcmath returns numbers with the number of digits according to $scale. This means that bcadd('2', '2', 6) will return '4.00000'. Trimming the number removes the excess zeroes.

Parameters

string $number: The number to trim.

Return value

string The trimmed number.

6 calls to Calculator::trim()
Calculator::add in src/Calculator.php
Adds the second number to the first number.
Calculator::divide in src/Calculator.php
Divides the first number by the second number.
Calculator::multiply in src/Calculator.php
Multiplies the first number by the second number.
Calculator::subtract in src/Calculator.php
Subtracts the second number from the first number.
Measurement::__toString in src/Measurement.php
Gets the string representation of the measurement.

... See full list

File

src/Calculator.php, line 238

Class

Calculator
Provides helpers for bcmath-based arithmetic.

Namespace

Drupal\physical

Code

public static function trim($number) {
  if (strpos($number, '.') != FALSE) {

    // The number is decimal, strip trailing zeroes.
    // If no digits remain after the decimal point, strip it as well.
    $number = rtrim($number, '0');
    $number = rtrim($number, '.');
  }
  return $number;
}