Skip to content

Commit 25680e7

Browse files
committed
Add ratio unit.
1 parent 857646a commit 25680e7

File tree

4 files changed

+425
-0
lines changed

4 files changed

+425
-0
lines changed

Src/UnitsNet/Attributes/Attributes.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ public PressureAttribute(double slope, string pluralName = (string)null) : base(
125125
}
126126
}
127127

128+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
129+
public class RatioAttribute : UnitAttribute, IUnitAttribute
130+
{
131+
public RatioUnit BaseUnit { get { return RatioUnit.DecimalFraction; } }
132+
public string BaseUnitName { get { return BaseUnit.ToString(); }}
133+
public string XmlDocSummary { get { return "In mathematics, a ratio is a relationship between two numbers of the same kind (e.g., objects, persons, students, spoonfuls, units of whatever identical dimension), usually expressed as \"a to b\" or a:b, sometimes expressed arithmetically as a dimensionless quotient of the two that explicitly indicates how many times the first number contains the second (not necessarily an integer)."; } }
134+
135+
public RatioAttribute(double fraction, string pluralName = (string)null) : base(pluralName, fraction, offset: 0)
136+
{
137+
}
138+
}
139+
128140
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
129141
public class RotationalSpeedAttribute : UnitAttribute, IUnitAttribute
130142
{
Lines changed: 366 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,366 @@
1+
// Copyright © 2007 by Initial Force AS. All rights reserved.
2+
// https://github.com/InitialForce/SIUnits
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
using System;
23+
using System.Globalization;
24+
using System.Linq;
25+
using UnitsNet.Units;
26+
27+
// ReSharper disable once CheckNamespace
28+
namespace UnitsNet
29+
{
30+
/// <summary>
31+
/// In mathematics, a ratio is a relationship between two numbers of the same kind (e.g., objects, persons, students, spoonfuls, units of whatever identical dimension), usually expressed as "a to b" or a:b, sometimes expressed arithmetically as a dimensionless quotient of the two that explicitly indicates how many times the first number contains the second (not necessarily an integer).
32+
/// </summary>
33+
public partial struct Ratio : IComparable, IComparable<Ratio>
34+
{
35+
/// <summary>
36+
/// Base unit of Ratio.
37+
/// </summary>
38+
public readonly double DecimalFractions;
39+
40+
public Ratio(double decimalfractions) : this()
41+
{
42+
DecimalFractions = decimalfractions;
43+
}
44+
45+
#region Properties
46+
47+
/// <summary>
48+
/// Get Ratio in PartsPerBillions.
49+
/// </summary>
50+
/// <remarks>Example: x = (y - b) / a where x is value in PartsPerBillions and y is value in base unit DecimalFractions.</remarks>
51+
public double PartsPerBillions
52+
{
53+
get { return DecimalFractions / 1E-09; }
54+
}
55+
56+
/// <summary>
57+
/// Get Ratio in PartsPerMillions.
58+
/// </summary>
59+
/// <remarks>Example: x = (y - b) / a where x is value in PartsPerMillions and y is value in base unit DecimalFractions.</remarks>
60+
public double PartsPerMillions
61+
{
62+
get { return DecimalFractions / 1E-06; }
63+
}
64+
65+
/// <summary>
66+
/// Get Ratio in PartsPerThousands.
67+
/// </summary>
68+
/// <remarks>Example: x = (y - b) / a where x is value in PartsPerThousands and y is value in base unit DecimalFractions.</remarks>
69+
public double PartsPerThousands
70+
{
71+
get { return DecimalFractions / 0.001; }
72+
}
73+
74+
/// <summary>
75+
/// Get Ratio in PartsPerTrillions.
76+
/// </summary>
77+
/// <remarks>Example: x = (y - b) / a where x is value in PartsPerTrillions and y is value in base unit DecimalFractions.</remarks>
78+
public double PartsPerTrillions
79+
{
80+
get { return DecimalFractions / 1E-12; }
81+
}
82+
83+
/// <summary>
84+
/// Get Ratio in Percents.
85+
/// </summary>
86+
/// <remarks>Example: x = (y - b) / a where x is value in Percents and y is value in base unit DecimalFractions.</remarks>
87+
public double Percents
88+
{
89+
get { return DecimalFractions / 0.01; }
90+
}
91+
92+
#endregion
93+
94+
#region Static
95+
96+
public static Ratio Zero
97+
{
98+
get { return new Ratio(); }
99+
}
100+
101+
/// <summary>
102+
/// Get Ratio from DecimalFractions.
103+
/// </summary>
104+
/// <remarks>Example: y = ax + b where x is value in DecimalFractions and y is value in base unit DecimalFractions.</remarks>
105+
public static Ratio FromDecimalFractions(double decimalfractions)
106+
{
107+
return new Ratio(1 * decimalfractions);
108+
}
109+
110+
/// <summary>
111+
/// Get Ratio from PartsPerBillions.
112+
/// </summary>
113+
/// <remarks>Example: y = ax + b where x is value in PartsPerBillions and y is value in base unit DecimalFractions.</remarks>
114+
public static Ratio FromPartsPerBillions(double partsperbillions)
115+
{
116+
return new Ratio(1E-09 * partsperbillions);
117+
}
118+
119+
/// <summary>
120+
/// Get Ratio from PartsPerMillions.
121+
/// </summary>
122+
/// <remarks>Example: y = ax + b where x is value in PartsPerMillions and y is value in base unit DecimalFractions.</remarks>
123+
public static Ratio FromPartsPerMillions(double partspermillions)
124+
{
125+
return new Ratio(1E-06 * partspermillions);
126+
}
127+
128+
/// <summary>
129+
/// Get Ratio from PartsPerThousands.
130+
/// </summary>
131+
/// <remarks>Example: y = ax + b where x is value in PartsPerThousands and y is value in base unit DecimalFractions.</remarks>
132+
public static Ratio FromPartsPerThousands(double partsperthousands)
133+
{
134+
return new Ratio(0.001 * partsperthousands);
135+
}
136+
137+
/// <summary>
138+
/// Get Ratio from PartsPerTrillions.
139+
/// </summary>
140+
/// <remarks>Example: y = ax + b where x is value in PartsPerTrillions and y is value in base unit DecimalFractions.</remarks>
141+
public static Ratio FromPartsPerTrillions(double partspertrillions)
142+
{
143+
return new Ratio(1E-12 * partspertrillions);
144+
}
145+
146+
/// <summary>
147+
/// Get Ratio from Percents.
148+
/// </summary>
149+
/// <remarks>Example: y = ax + b where x is value in Percents and y is value in base unit DecimalFractions.</remarks>
150+
public static Ratio FromPercents(double percents)
151+
{
152+
return new Ratio(0.01 * percents);
153+
}
154+
155+
/// <summary>
156+
/// Try to dynamically convert from Ratio to <paramref name="toUnit"/>.
157+
/// </summary>
158+
/// <param name="value">Value to convert from.</param>
159+
/// <param name="fromUnit">Unit to convert from.</param>
160+
/// <returns>Ratio unit value.</returns>
161+
public static Ratio From(double value, RatioUnit fromUnit)
162+
{
163+
switch (fromUnit)
164+
{
165+
case RatioUnit.DecimalFraction:
166+
return FromDecimalFractions(value);
167+
case RatioUnit.PartsPerBillion:
168+
return FromPartsPerBillions(value);
169+
case RatioUnit.PartsPerMillion:
170+
return FromPartsPerMillions(value);
171+
case RatioUnit.PartsPerThousand:
172+
return FromPartsPerThousands(value);
173+
case RatioUnit.PartsPerTrillion:
174+
return FromPartsPerTrillions(value);
175+
case RatioUnit.Percent:
176+
return FromPercents(value);
177+
178+
default:
179+
throw new NotImplementedException("fromUnit: " + fromUnit);
180+
}
181+
}
182+
183+
/// <summary>
184+
/// Get unit abbreviation string.
185+
/// </summary>
186+
/// <param name="unit">Unit to get abbreviation for.</param>
187+
/// <param name="culture">Culture to use for localization. Defaults to Thread.CurrentUICulture.</param>
188+
/// <returns>Unit abbreviation string.</returns>
189+
public static string GetAbbreviation(RatioUnit unit, CultureInfo culture = null)
190+
{
191+
return UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit);
192+
}
193+
194+
#endregion
195+
196+
#region Arithmetic Operators
197+
198+
public static Ratio operator -(Ratio right)
199+
{
200+
return new Ratio(-right.DecimalFractions);
201+
}
202+
203+
public static Ratio operator +(Ratio left, Ratio right)
204+
{
205+
return new Ratio(left.DecimalFractions + right.DecimalFractions);
206+
}
207+
208+
public static Ratio operator -(Ratio left, Ratio right)
209+
{
210+
return new Ratio(left.DecimalFractions - right.DecimalFractions);
211+
}
212+
213+
public static Ratio operator *(double left, Ratio right)
214+
{
215+
return new Ratio(left*right.DecimalFractions);
216+
}
217+
218+
public static Ratio operator *(Ratio left, double right)
219+
{
220+
return new Ratio(left.DecimalFractions*right);
221+
}
222+
223+
public static Ratio operator /(Ratio left, double right)
224+
{
225+
return new Ratio(left.DecimalFractions/right);
226+
}
227+
228+
public static double operator /(Ratio left, Ratio right)
229+
{
230+
return left.DecimalFractions/right.DecimalFractions;
231+
}
232+
233+
#endregion
234+
235+
#region Equality / IComparable
236+
237+
public int CompareTo(object obj)
238+
{
239+
if (obj == null) throw new ArgumentNullException("obj");
240+
if (!(obj is Ratio)) throw new ArgumentException("Expected type Ratio.", "obj");
241+
return CompareTo((Ratio) obj);
242+
}
243+
244+
public int CompareTo(Ratio other)
245+
{
246+
return DecimalFractions.CompareTo(other.DecimalFractions);
247+
}
248+
249+
public static bool operator <=(Ratio left, Ratio right)
250+
{
251+
return left.DecimalFractions <= right.DecimalFractions;
252+
}
253+
254+
public static bool operator >=(Ratio left, Ratio right)
255+
{
256+
return left.DecimalFractions >= right.DecimalFractions;
257+
}
258+
259+
public static bool operator <(Ratio left, Ratio right)
260+
{
261+
return left.DecimalFractions < right.DecimalFractions;
262+
}
263+
264+
public static bool operator >(Ratio left, Ratio right)
265+
{
266+
return left.DecimalFractions > right.DecimalFractions;
267+
}
268+
269+
public static bool operator ==(Ratio left, Ratio right)
270+
{
271+
return left.DecimalFractions == right.DecimalFractions;
272+
}
273+
274+
public static bool operator !=(Ratio left, Ratio right)
275+
{
276+
return left.DecimalFractions != right.DecimalFractions;
277+
}
278+
279+
public override bool Equals(object obj)
280+
{
281+
if (obj == null || GetType() != obj.GetType())
282+
{
283+
return false;
284+
}
285+
286+
return DecimalFractions.Equals(((Ratio) obj).DecimalFractions);
287+
}
288+
289+
public override int GetHashCode()
290+
{
291+
return DecimalFractions.GetHashCode();
292+
}
293+
294+
#endregion
295+
296+
#region Conversion
297+
298+
/// <summary>
299+
/// Convert to the unit representation in <paramref name="asUnit"/>.
300+
/// </summary>
301+
/// <param name="toUnit">Compatible unit to convert to.</param>
302+
/// <returns>Value in new unit if successful, exception otherwise.</returns>
303+
/// <exception cref="NotImplementedException">If conversion was not successful.</exception>
304+
public double As(RatioUnit unit)
305+
{
306+
switch (unit)
307+
{
308+
case RatioUnit.DecimalFraction:
309+
return DecimalFractions;
310+
case RatioUnit.PartsPerBillion:
311+
return PartsPerBillions;
312+
case RatioUnit.PartsPerMillion:
313+
return PartsPerMillions;
314+
case RatioUnit.PartsPerThousand:
315+
return PartsPerThousands;
316+
case RatioUnit.PartsPerTrillion:
317+
return PartsPerTrillions;
318+
case RatioUnit.Percent:
319+
return Percents;
320+
321+
default:
322+
throw new NotImplementedException("unit: " + unit);
323+
}
324+
}
325+
326+
#endregion
327+
328+
/// <summary>
329+
/// Get string representation of value and unit.
330+
/// </summary>
331+
/// <param name="culture">Culture to use for localization and number formatting.</param>
332+
/// <param name="unit">Unit representation to use.</param>
333+
/// <returns>String representation.</returns>
334+
public string ToString(RatioUnit unit, CultureInfo culture = null)
335+
{
336+
return ToString(unit, culture, "{0:0.##} {1}");
337+
}
338+
339+
/// <summary>
340+
/// Get string representation of value and unit.
341+
/// </summary>
342+
/// <param name="culture">Culture to use for localization and number formatting.</param>
343+
/// <param name="unit">Unit representation to use.</param>
344+
/// <param name="format">String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively."</param>
345+
/// <param name="args">Arguments for string format. Value and unit are implictly included as arguments 0 and 1.</param>
346+
/// <returns>String representation.</returns>
347+
public string ToString(RatioUnit unit, CultureInfo culture, string format, params object[] args)
348+
{
349+
string abbreviation = UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit);
350+
var finalArgs = new object[] {As(unit), abbreviation}
351+
.Concat(args)
352+
.ToArray();
353+
354+
return string.Format(culture, format, finalArgs);
355+
}
356+
357+
/// <summary>
358+
/// Get default string representation of value and unit.
359+
/// </summary>
360+
/// <returns>String representation.</returns>
361+
public override string ToString()
362+
{
363+
return ToString(RatioUnit.DecimalFraction);
364+
}
365+
}
366+
}

0 commit comments

Comments
 (0)