Skip to content

Commit

Permalink
Fix a bug in BaseDimensions.ToString() (#1431)
Browse files Browse the repository at this point in the history
Current implementation of `AppendDimensionString` ignores value `-1`.
This PR fixes the bug.

---------

Co-authored-by: ygorshkov <[email protected]>
  • Loading branch information
ygorshkov and ygorshkov authored Oct 23, 2024
1 parent 5f0f22d commit b57ac63
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 7 additions & 1 deletion UnitsNet.Tests/BaseDimensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,13 @@ public void CheckBaseDimensionMultiplicationWithForceEqualsMassTimesAcceleration
[Fact]
public void CheckToStringUsingMolarEntropy()
{
Assert.Equal("[Length]^2[Mass][Time]^-2[Temperature][Amount]", MolarEntropy.BaseDimensions.ToString());
Assert.Equal("[Length]^2[Mass][Time]^-2[Temperature]^-1[Amount]^-1", MolarEntropy.BaseDimensions.ToString());
}

[Fact]
public void CheckToStringUsingSpeed()
{
Assert.Equal("[Length][Time]^-1", Speed.BaseDimensions.ToString());
}

[Fact]
Expand Down
6 changes: 2 additions & 4 deletions UnitsNet/BaseDimensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,11 @@ public override string ToString()

private static void AppendDimensionString(StringBuilder sb, string name, int value)
{
var absoluteValue = Math.Abs(value);

if(absoluteValue > 0)
if (0 != value)
{
sb.AppendFormat("[{0}]", name);

if(absoluteValue > 1)
if (1 != value)
sb.AppendFormat("^{0}", value);
}
}
Expand Down

0 comments on commit b57ac63

Please sign in to comment.