convertTo<E extends Unit<T>> method

T convertTo<E extends Unit<T>>(
  1. E to
)

Convert this unit to another unit under same category, the value of to is ignored

Implementation

T convertTo<E extends Unit<T>>(E to) {
  final result = to as T;
  if (runtimeType == to.runtimeType) {
    return result.withValue(value);
  }
  if (_isShiftedValue || result._isShiftedValue) {
    if (runtimeType == _anchorRatio.anchor) {
      return result.withValue(
        (value - result.valueShift) /
            _anchorRatio.ratio.getRatio(result.runtimeType),
      );
    } else {
      return anchor
          .withValue(
            (value * _anchorRatio.ratio.getRatio(runtimeType)) + valueShift,
          )
          .convertTo(result);
    }
  } else {
    if (value == 0) {
      return result.withValue(0);
    }
    if (runtimeType == _anchorRatio.anchor) {
      return result
          .withValue(value / _anchorRatio.ratio.getRatio(result.runtimeType));
    }
    return anchor
        .withValue(value * _anchorRatio.ratio.getRatio(runtimeType))
        .convertTo(result);
  }
}