You are here

public function Measurement::convert in Physical Fields 8

Converts the current measurement to a new unit.

Parameters

string $new_unit: The new unit.

Return value

static The resulting length.

1 method overrides Measurement::convert()
Temperature::convert in src/Temperature.php
Converts the current temperature measurement to a new unit.

File

src/Measurement.php, line 100

Class

Measurement
Provides a base class for measurement value objects.

Namespace

Drupal\physical

Code

public function convert($new_unit) {

  /** @var \Drupal\physical\UnitInterface $unit_class */
  $unit_class = MeasurementType::getUnitClass($this->type);

  // Convert the number to the base unit, then from there to the new unit.
  $base_number = Calculator::multiply($this->number, $unit_class::getBaseFactor($this->unit));
  $new_number = Calculator::divide($base_number, $unit_class::getBaseFactor($new_unit));
  return new static($new_number, $new_unit);
}