Skip to content

Commit b39b3d7

Browse files
Add operator Energy divided by time to get Power (#1057)
Co-authored-by: Marnix <[email protected]>
1 parent f655dce commit b39b3d7

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

UnitsNet.Tests/CustomCode/EnergyTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

4+
using System;
45
using UnitsNet.Units;
56
using Xunit;
67

@@ -105,5 +106,19 @@ public void ToUnit_GivenSIUnitSystem_ReturnsSIQuantity()
105106
Assert.Equal(2110.11170524, inSI.Value);
106107
Assert.Equal(EnergyUnit.Joule, inSI.Unit);
107108
}
109+
110+
[Fact]
111+
public void EnergyDividedByTimeSpanEqualsPower()
112+
{
113+
Power p = Energy.FromWattHours(10) / TimeSpan.FromHours(2);
114+
Assert.Equal(5, p.Watts);
115+
}
116+
117+
[Fact]
118+
public void EnergyDividedByDurationEqualsPower()
119+
{
120+
Power p = Energy.FromWattHours(20) / Duration.FromHours(5);
121+
Assert.Equal(4, p.Watts);
122+
}
108123
}
109124
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
2+
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
3+
4+
using System;
5+
6+
namespace UnitsNet
7+
{
8+
public partial struct Energy
9+
{
10+
/// <summary>Get <see cref="Power"/> from <see cref="Energy"/> divided by <see cref="TimeSpan"/>.</summary>
11+
public static Power operator /(Energy energy, TimeSpan time)
12+
{
13+
return Power.FromWatts(energy.Joules / time.TotalSeconds);
14+
}
15+
16+
/// <summary>Get <see cref="Power"/> from <see cref="Energy"/> divided by <see cref="Duration"/>.</summary>
17+
public static Power operator /(Energy energy, Duration duration)
18+
{
19+
return Power.FromWatts(energy.Joules / duration.Seconds);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)