Skip to content

Commit d663b90

Browse files
committed
Add conversion to Megavolts
+ Add conversions for Megavolts (MV).
1 parent de5d494 commit d663b90

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/voltage.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ impl Voltage {
4242
Self::from_volts(kilovolts * 1000.0)
4343
}
4444

45+
/// Create a new Voltage from a floating point value in Megavolts
46+
pub fn from_megavolts(megavolts: f64) -> Self {
47+
Self::from_volts(megavolts * 1000000.0)
48+
}
49+
50+
4551
/// Convert this Voltage into a floating point value in Volts
4652
pub fn as_volts(&self) -> f64 {
4753
self.volts
@@ -61,6 +67,11 @@ impl Voltage {
6167
pub fn as_kilovolts(&self) -> f64 {
6268
self.volts / 1000.0
6369
}
70+
71+
/// Convert this Voltage into a floating point value in Megavolts
72+
pub fn as_megavolts(&self) -> f64 {
73+
self.volts / 1000000.0
74+
}
6475
}
6576

6677
impl Measurement for Voltage {
@@ -108,6 +119,12 @@ mod test {
108119
assert_almost_eq(u.as_kilovolts(), 10.0);
109120
}
110121

122+
#[test]
123+
pub fn as_megavolts() {
124+
let u = Voltage::from_volts(10_000_000.0);
125+
assert_almost_eq(u.as_megavolts(), 10.0);
126+
}
127+
111128
#[test]
112129
pub fn as_volts() {
113130
let u = Voltage::from_kilovolts(1.234);

0 commit comments

Comments
 (0)