Skip to content

Instantly share code, notes, and snippets.

@eseidel
Created October 27, 2022 21:00
Show Gist options
  • Save eseidel/76cb2bdb7602be348b3aa9180e880e71 to your computer and use it in GitHub Desktop.
Save eseidel/76cb2bdb7602be348b3aa9180e880e71 to your computer and use it in GitHub Desktop.
class Pressure {
final double nPa; // nanopascals e-9
const Pressure.nPa(this.nPa);
const Pressure.uPa(double uPa) : nPa = uPa * 1000;
const Pressure.mPa(double mPa) : nPa = mPa * 1000000;
const Pressure.zero() : nPa = 0;
const Pressure.max() : nPa = double.maxFinite;
Pressure operator +(Pressure other) => Pressure.nPa(nPa + other.nPa);
Pressure operator -(Pressure other) => Pressure.nPa(nPa - other.nPa);
Pressure scaleBy(double multiplier) => Pressure.nPa(nPa * multiplier);
Pressure operator *(Pressure other) => Pressure.nPa(nPa * other.nPa);
bool operator >=(Pressure other) => nPa >= other.nPa;
Ti toTi() => Ti(nPa);
static const double microMultiplier = 1000;
static const double milliMultiplier = 1000000;
@override
String toString() {
var value = nPa;
var suffix = "nPa";
if (value >= milliMultiplier) {
value /= milliMultiplier;
suffix = 'mPa';
} else if (value >= microMultiplier) {
value /= microMultiplier;
suffix = 'uPa';
}
return "${value.toStringAsFixed(1)}$suffix";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment