Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added implicit conversion from Scalar to double #977

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,13 +545,20 @@ public void ToString_NullProvider_EqualsCurrentUICulture()
Assert.Equal(quantity.ToString(CultureInfo.CurrentUICulture, ""g""), quantity.ToString(null, ""g""));
}}

");
if (!_quantity.Name.Equals("Scalar"))
{
Writer.WL($@"

[Fact]
public void Convert_ToBool_ThrowsInvalidCastException()
{{
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(1.0);
Assert.Throws<InvalidCastException>(() => Convert.ToBoolean(quantity));
}}
");
}
Writer.WL($@"

[Fact]
public void Convert_ToByte_EqualsValueAsSameType()
Expand Down
14 changes: 13 additions & 1 deletion UnitsNet.Tests/CustomCode/ScalarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ public class ScalarTests : ScalarTestsBase
// Override properties in base class here
protected override double AmountInOneAmount => 1;
protected override bool SupportsSIUnitSystem => false;


[Theory]
[InlineData(0)]
[InlineData(1.0)]
[InlineData(-Math.PI)]
public void ImplicitCast_ToDouble_IsAmount(double amount)
{
var scalar = Scalar.FromAmount(amount);
double asDouble = scalar;

Assert.Equal(asDouble, scalar.Amount);
}
}

}
7 changes: 0 additions & 7 deletions UnitsNet.Tests/GeneratedCode/TestsBase/ScalarTestsBase.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions UnitsNet/CustomCode/Quantities/Scalar.extra.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.

namespace UnitsNet
{
public partial struct Scalar
{
/// <summary>Defines an implicit conversion of a <see cref="Scalar"/> to a <see cref="double"/>.</summary>
public static implicit operator double(Scalar scalar)
{
return scalar.Amount;
}
}
}