Converting from same temperature unit to same temperature unit assumes output temperature unit

Created on 18 February 2020, almost 5 years ago
Updated 5 August 2024, 6 months ago

For example, converting temperature from Fahrenheit to Fahrenheit assumes I want to convert to Kelvin. Wait, you may ask why would I convert from the same unit to the same unit. Well in my use case the user is selecting the input unit for the field. I created a custom formatter to display the temperature as multiple units in the front end.

Here is a sample of the code:

Custom Formatter:

$source->convert(TemperatureUnit::FAHRENHEIT);

web/modules/custom/symmetri_physical/src/Plugin/Field/FieldFormatter/MeasurementTemperatureRangeFormatter.php

   public function convert($new_unit) {
...
      case TemperatureUnit::FAHRENHEIT:
        if ($new_unit == TemperatureUnit::CELSIUS) {
          // @see http://www.rapidtables.com/convert/temperature/fahrenheit-to-celsius.htm
          $new_number = Calculator::divide(Calculator::subtract($this->number, '32'), '1.8');
        }
        else {
          // http://www.rapidtables.com/convert/temperature/fahrenheit-to-kelvin.htm
          $new_number = Calculator::divide(Calculator::add($this->number, '459.67'), '1.8');
        }
        break;
    }

I will submit a patch shortly.

πŸ› Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States philltran Detroit, MI

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024