You are here

public static function Calculator::trim in Commerce Core 8.2

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 modules/price/src/Calculator.php
Adds the second number to the first number.
Calculator::divide in modules/price/src/Calculator.php
Divides the first number by the second number.
Calculator::multiply in modules/price/src/Calculator.php
Multiplies the first number by the second number.
Calculator::subtract in modules/price/src/Calculator.php
Subtracts the second number from the first number.
CalculatorTest::testArithmetic in modules/price/tests/src/Unit/CalculatorTest.php
@covers ::add @covers ::subtract @covers ::multiply @covers ::divide @covers ::compare @covers ::trim

... See full list

File

modules/price/src/Calculator.php, line 236

Class

Calculator
Provides helpers for bcmath-based arithmetic.

Namespace

Drupal\commerce_price

Code

public static function trim(string $number) : string {
  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;
}