You are here

public static function Calculator::trim in Price 8

Same name and namespace in other branches
  1. 3.x src/Calculator.php \Drupal\price\Calculator::trim()
  2. 2.0.x src/Calculator.php \Drupal\price\Calculator::trim()
  3. 2.x src/Calculator.php \Drupal\price\Calculator::trim()
  4. 3.0.x src/Calculator.php \Drupal\price\Calculator::trim()

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.
Price::__toString in src/Price.php
Gets the string representation of the price.

... See full list

File

src/Calculator.php, line 236

Class

Calculator
Provides helpers for bcmath-based arithmetic.

Namespace

Drupal\price

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