diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6fb90d3..f0c7745 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Nothing yet.
+## [1.21.4] - 2026-03-10
+
+### Added
+
+- Getting decimals and doubles from environment variables.
+
## [1.21.3] - 2026-01-23
### Fixed
@@ -317,7 +323,8 @@ Nothing yet.
- Implemented StringExtensions.
-[unreleased]: https://github.com/Logitar/Logitar.NET/compare/v1.21.3...HEAD
+[unreleased]: https://github.com/Logitar/Logitar.NET/compare/v1.21.4...HEAD
+[1.21.4]: https://github.com/Logitar/Logitar.NET/compare/v1.21.3...v1.21.4
[1.21.3]: https://github.com/Logitar/Logitar.NET/compare/v1.21.2...v1.21.3
[1.21.2]: https://github.com/Logitar/Logitar.NET/compare/v1.21.1...v1.21.2
[1.21.1]: https://github.com/Logitar/Logitar.NET/compare/v1.21.0...v1.21.1
diff --git a/src/Logitar/EnvironmentHelper.cs b/src/Logitar/EnvironmentHelper.cs
index c9bb531..37cc0fd 100644
--- a/src/Logitar/EnvironmentHelper.cs
+++ b/src/Logitar/EnvironmentHelper.cs
@@ -23,6 +23,42 @@ public static class EnvironmentHelper
return !string.IsNullOrWhiteSpace(value) && bool.TryParse(value.Trim(), out bool boolean) ? boolean : null;
}
+ ///
+ /// Retrieves the decimal value of an environment variable.
+ ///
+ /// The name of the variable.
+ /// The default value.
+ /// The value of the environment value.
+ public static decimal GetDecimal(string variable, decimal defaultValue = 0m) => TryGetDecimal(variable) ?? defaultValue;
+ ///
+ /// Retrieves the decimal value of an environment variable. Returns null if it is not found.
+ ///
+ /// The name of the variable.
+ /// The value of the environment value, or null if not found.
+ public static decimal? TryGetDecimal(string variable)
+ {
+ string value = Environment.GetEnvironmentVariable(variable);
+ return !string.IsNullOrWhiteSpace(value) && decimal.TryParse(value.Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out decimal number) ? number : null;
+ }
+
+ ///
+ /// Retrieves the double value of an environment variable.
+ ///
+ /// The name of the variable.
+ /// The default value.
+ /// The value of the environment value.
+ public static double GetDouble(string variable, double defaultValue = 0d) => TryGetDouble(variable) ?? defaultValue;
+ ///
+ /// Retrieves the double value of an environment variable. Returns null if it is not found.
+ ///
+ /// The name of the variable.
+ /// The value of the environment value, or null if not found.
+ public static double? TryGetDouble(string variable)
+ {
+ string value = Environment.GetEnvironmentVariable(variable);
+ return !string.IsNullOrWhiteSpace(value) && double.TryParse(value.Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out double number) ? number : null;
+ }
+
///
/// Retrieves the enum value of an environment variable.
///
@@ -59,7 +95,7 @@ public static T GetEnum(string variable, T defaultValue = default) where T :
public static int? TryGetInt32(string variable)
{
string value = Environment.GetEnvironmentVariable(variable);
- return !string.IsNullOrWhiteSpace(value) && int.TryParse(value.Trim(), out int integer) ? integer : null;
+ return !string.IsNullOrWhiteSpace(value) && int.TryParse(value.Trim(), out int number) ? number : null;
}
///
diff --git a/src/Logitar/Logitar.csproj b/src/Logitar/Logitar.csproj
index c322611..b5539b6 100644
--- a/src/Logitar/Logitar.csproj
+++ b/src/Logitar/Logitar.csproj
@@ -15,14 +15,14 @@
README.md
git
https://github.com/Logitar/Logitar.NET
- 10.1.1.0
+ 10.2.0.0
$(AssemblyVersion)
LICENSE
True
- 10.1.1
+ 10.2.0
en-CA
True
- Fixed license year.
+ Getting decimals and doubles from environment variables.
logitar net framework
https://github.com/Logitar/Logitar.NET/tree/dev/src/Logitar
diff --git a/tests/Logitar.UnitTests/EnvironmentHelperTests.cs b/tests/Logitar.UnitTests/EnvironmentHelperTests.cs
index b960235..ed53fc2 100644
--- a/tests/Logitar.UnitTests/EnvironmentHelperTests.cs
+++ b/tests/Logitar.UnitTests/EnvironmentHelperTests.cs
@@ -25,9 +25,45 @@ public void Given_NotFound_When_GetBoolean_Then_DefaultValue(bool defaultValue)
Assert.Equal(defaultValue, variable);
}
+ [Fact(DisplayName = "GetDecimal: it should return the found variable value.")]
+ public void Given_Found_When_GetDecimal_Then_DecimalValue()
+ {
+ decimal value = 3.14159m;
+ Environment.SetEnvironmentVariable(VariableName, value.ToString());
+ decimal variable = EnvironmentHelper.GetDecimal(VariableName);
+ Assert.Equal(value, variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Fact(DisplayName = "GetDecimal: it should return the default variable value.")]
+ public void Given_NotFound_When_GetDecimal_Then_DefaultValue()
+ {
+ decimal defaultValue = -1m;
+ decimal variable = EnvironmentHelper.GetDecimal(VariableName, defaultValue);
+ Assert.Equal(defaultValue, variable);
+ }
+
+ [Fact(DisplayName = "GetDouble: it should return the found variable value.")]
+ public void Given_Found_When_GetDouble_Then_DoubleValue()
+ {
+ double value = 3.14159d;
+ Environment.SetEnvironmentVariable(VariableName, value.ToString());
+ double variable = EnvironmentHelper.GetDouble(VariableName);
+ Assert.Equal(value, variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Fact(DisplayName = "GetDouble: it should return the default variable value.")]
+ public void Given_NotFound_When_GetDouble_Then_DefaultValue()
+ {
+ double defaultValue = -1d;
+ double variable = EnvironmentHelper.GetDouble(VariableName, defaultValue);
+ Assert.Equal(defaultValue, variable);
+ }
+
[Theory(DisplayName = "GetEnum: it should return the found variable value.")]
[InlineData(DayOfWeek.Sunday)]
- public void Given_Found_When_GetEnum_Then_BooleanValue(DayOfWeek value)
+ public void Given_Found_When_GetEnum_Then_EnumValue(DayOfWeek value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
DayOfWeek variable = EnvironmentHelper.GetEnum(VariableName);
@@ -45,7 +81,7 @@ public void Given_NotFound_When_GetEnum_Then_DefaultValue(DayOfWeek defaultValue
[Theory(DisplayName = "GetInt32: it should return the found variable value.")]
[InlineData(42)]
- public void Given_Found_When_GetInt32_Then_BooleanValue(int value)
+ public void Given_Found_When_GetInt32_Then_IntegerValue(int value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
int variable = EnvironmentHelper.GetInt32(VariableName);
@@ -63,7 +99,7 @@ public void Given_NotFound_When_GetInt32_Then_DefaultValue(int defaultValue)
[Theory(DisplayName = "GetString: it should return the found variable value.")]
[InlineData(" Test ")]
- public void Given_Found_When_GetString_Then_BooleanValue(string value)
+ public void Given_Found_When_GetString_Then_StringValue(string value)
{
Environment.SetEnvironmentVariable(VariableName, value);
string variable = EnvironmentHelper.GetString(VariableName);
@@ -81,7 +117,7 @@ public void Given_NotFound_When_GetString_Then_DefaultValue(string defaultValue)
[Theory(DisplayName = "GetTimeSpan: it should return the found variable value.")]
[InlineData(" 12:34:56 ")]
- public void Given_Found_When_GetTimeSpan_Then_BooleanValue(string value)
+ public void Given_Found_When_GetTimeSpan_Then_TimeSpanValue(string value)
{
TimeSpan timeSpan = TimeSpan.Parse(value);
Environment.SetEnvironmentVariable(VariableName, value);
@@ -112,14 +148,14 @@ public void Given_Found_When_TryGetBoolean_Then_BooleanValue(bool value)
}
[Fact(DisplayName = "TryGetBoolean: it should return null when the variable is not found.")]
- public void Given_NotFound_When_TryGetBoolean_Then_BooleanValue()
+ public void Given_NotFound_When_TryGetBoolean_Then_NullValue()
{
Assert.Null(EnvironmentHelper.TryGetBoolean(VariableName));
}
[Theory(DisplayName = "TryGetEnum: it should return the found variable value.")]
[InlineData(DayOfWeek.Saturday)]
- public void Given_Found_When_TryGetEnum_Then_BooleanValue(DayOfWeek value)
+ public void Given_Found_When_TryGetEnum_Then_EnumValue(DayOfWeek value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
DayOfWeek? variable = EnvironmentHelper.TryGetEnum(VariableName);
@@ -129,14 +165,48 @@ public void Given_Found_When_TryGetEnum_Then_BooleanValue(DayOfWeek value)
}
[Fact(DisplayName = "TryGetEnum: it should return null when the variable is not found.")]
- public void Given_NotFound_When_TryGetEnum_Then_BooleanValue()
+ public void Given_NotFound_When_TryGetEnum_Then_Nullalue()
{
Assert.Null(EnvironmentHelper.TryGetEnum(VariableName));
}
+ [Fact(DisplayName = "TryGetDecimal: it should return the found variable value.")]
+ public void Given_Found_When_TryGetDecimal_Then_IntegerValue()
+ {
+ decimal value = 3.14159m;
+ Environment.SetEnvironmentVariable(VariableName, value.ToString());
+ decimal? variable = EnvironmentHelper.TryGetDecimal(VariableName);
+ Assert.NotNull(variable);
+ Assert.Equal(value, variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Fact(DisplayName = "TryGetDecimal: it should return null when the variable is not found.")]
+ public void Given_NotFound_When_TryGetDecimal_Then_NullValue()
+ {
+ Assert.Null(EnvironmentHelper.TryGetDecimal(VariableName));
+ }
+
+ [Fact(DisplayName = "TryGetDouble: it should return the found variable value.")]
+ public void Given_Found_When_TryGetDouble_Then_IntegerValue()
+ {
+ double value = 3.14159d;
+ Environment.SetEnvironmentVariable(VariableName, value.ToString());
+ double? variable = EnvironmentHelper.TryGetDouble(VariableName);
+ Assert.NotNull(variable);
+ Assert.Equal(value, variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Fact(DisplayName = "TryGetDouble: it should return null when the variable is not found.")]
+ public void Given_NotFound_When_TryGetDouble_Then_NullValue()
+ {
+ Assert.Null(EnvironmentHelper.TryGetDouble(VariableName));
+ }
+
[Theory(DisplayName = "TryGetInt32: it should return the found variable value.")]
[InlineData(999)]
- public void Given_Found_When_TryGetInt32_Then_BooleanValue(int value)
+ public void Given_Found_When_TryGetInt32_Then_IntegerValue(int value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
int? variable = EnvironmentHelper.TryGetInt32(VariableName);
@@ -146,14 +216,14 @@ public void Given_Found_When_TryGetInt32_Then_BooleanValue(int value)
}
[Fact(DisplayName = "TryGetInt32: it should return null when the variable is not found.")]
- public void Given_NotFound_When_TryGetInt32_Then_BooleanValue()
+ public void Given_NotFound_When_TryGetInt32_Then_NullValue()
{
Assert.Null(EnvironmentHelper.TryGetInt32(VariableName));
}
[Theory(DisplayName = "TryGetString: it should return the found variable value.")]
[InlineData(" Test ")]
- public void Given_Found_When_TryGetString_Then_BooleanValue(string value)
+ public void Given_Found_When_TryGetString_Then_StringValue(string value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
string? variable = EnvironmentHelper.TryGetString(VariableName);
@@ -163,14 +233,14 @@ public void Given_Found_When_TryGetString_Then_BooleanValue(string value)
}
[Fact(DisplayName = "TryGetString: it should return null when the variable is not found.")]
- public void Given_NotFound_When_TryGetString_Then_BooleanValue()
+ public void Given_NotFound_When_TryGetString_Then_NullValue()
{
Assert.Null(EnvironmentHelper.TryGetString(VariableName));
}
[Theory(DisplayName = "TryGetTimeSpan: it should return the found variable value.")]
[InlineData(" 7.00:00:00 ")]
- public void Given_Found_When_TryGetTimeSpan_Then_BooleanValue(string value)
+ public void Given_Found_When_TryGetTimeSpan_Then_TimeSpanValue(string value)
{
Environment.SetEnvironmentVariable(VariableName, value);
TimeSpan? variable = EnvironmentHelper.TryGetTimeSpan(VariableName);
@@ -180,7 +250,7 @@ public void Given_Found_When_TryGetTimeSpan_Then_BooleanValue(string value)
}
[Fact(DisplayName = "TryGetTimeSpan: it should return null when the variable is not found.")]
- public void Given_NotFound_When_TryGetTimeSpan_Then_BooleanValue()
+ public void Given_NotFound_When_TryGetTimeSpan_Then_NullValue()
{
Assert.Null(EnvironmentHelper.TryGetTimeSpan(VariableName));
}