Skip to content

Commit 51e77b7

Browse files
committed
Add conversion to Megawatts
+ Add conversions to Megawatts (MW).
1 parent d663b90 commit 51e77b7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/power.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub const WATT_HORSEPOWER_FACTOR: f64 = 1.0 / 745.6998715822702;
88
pub const WATT_BTU_MIN_FACTOR: f64 = 1.0 / 17.58426666666667;
99
/// Number of kW in a W
1010
pub const WATT_KILOWATT_FACTOR: f64 = 1e-3;
11+
/// Number of MW in a W
12+
pub const WATT_MEGAWATT_FACTOR: f64 = 1e-6;
1113
/// Number of mW in a W
1214
pub const WATT_MILLIWATT_FACTOR: f64 = 1e3;
1315
/// Number of µW in a W
@@ -74,6 +76,11 @@ impl Power {
7476
Self::from_watts(kw / WATT_KILOWATT_FACTOR)
7577
}
7678

79+
/// Create a new Power from a floating point value in Megawatts (kW)
80+
pub fn from_megawatts(kw: f64) -> Power {
81+
Self::from_watts(kw / WATT_MEGAWATT_FACTOR)
82+
}
83+
7784
/// Convert this Power into a floating point value in Watts
7885
pub fn as_watts(&self) -> f64 {
7986
self.watts
@@ -104,6 +111,11 @@ impl Power {
104111
self.watts * WATT_KILOWATT_FACTOR
105112
}
106113

114+
/// Convert this Power into a floating point value in megawatts (MW)
115+
pub fn as_megawatts(&self) -> f64 {
116+
self.watts * WATT_MEGAWATT_FACTOR
117+
}
118+
107119
/// Convert this Power into a floating point value in milliwatts (mW)
108120
pub fn as_milliwatts(&self) -> f64 {
109121
self.watts * WATT_MILLIWATT_FACTOR
@@ -190,6 +202,18 @@ mod test {
190202
assert_almost_eq(r2, 0.1);
191203
}
192204

205+
#[test]
206+
pub fn as_megawatts() {
207+
let i1 = Power::from_megawatts(100.0);
208+
let r1 = i1.as_watts();
209+
210+
let i2 = Power::from_watts(100_000.0);
211+
let r2 = i2.as_megawatts();
212+
213+
assert_almost_eq(r1, 100_000_000.0);
214+
assert_almost_eq(r2, 0.1);
215+
}
216+
193217
#[test]
194218
pub fn as_milliwatts() {
195219
let i1 = Power::from_milliwatts(100.0);

0 commit comments

Comments
 (0)