Skip to content

Commit

Permalink
QuantityFormatter defaults to the "G" format (#1450)
Browse files Browse the repository at this point in the history
- [x] `QuantityFormatter` defaults to the "G" format for null or empty
string
- [x] `QuantityFormatter` no longer supports the `U`/`V`/`Q` formats (an
exception is thrown)
- [x] `QuantityFormatter` explicitly throws for the `Cx`/`Px` formats
- [x] `QuantityFormatter` shouldn't throw for something like "P1: #.00"
(instead it should output something like "P1: 12.34 mg")
- [x] `QuantityFormatter` unless explicitly given an incorrect `Ax`
specifier, the `QuantityFormatter` uses the "default unit abbreviation"
- which maybe `string.Empty` (if none are defined for a given unit)

Fixes #1183 
Duplicate of #1206

CC @tmilnthorp

---------

Co-authored-by: Andreas Gullberg Larsen <[email protected]>
  • Loading branch information
lipchev and angularsen authored Dec 15, 2024
1 parent 50ba1c1 commit 095c85b
Show file tree
Hide file tree
Showing 256 changed files with 1,012 additions and 868 deletions.
8 changes: 4 additions & 4 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ private void GenerateToString()
/// <returns>String representation.</returns>
public override string ToString()
{{
return ToString(""g"");
return ToString(null, null);
}}
/// <summary>
Expand All @@ -1180,7 +1180,7 @@ public override string ToString()
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public string ToString(IFormatProvider? provider)
{{
return ToString(""g"", provider);
return ToString(null, provider);
}}
/// <inheritdoc cref=""QuantityFormatter.Format{{TUnitType}}(IQuantity{{TUnitType}}, string, IFormatProvider)""/>
Expand All @@ -1191,7 +1191,7 @@ public string ToString(IFormatProvider? provider)
/// <returns>The string representation.</returns>
public string ToString(string? format)
{{
return ToString(format, CultureInfo.CurrentCulture);
return ToString(format, null);
}}
/// <inheritdoc cref=""QuantityFormatter.Format{{TUnitType}}(IQuantity{{TUnitType}}, string, IFormatProvider)""/>
Expand Down Expand Up @@ -1277,7 +1277,7 @@ float IConvertible.ToSingle(IFormatProvider? provider)
string IConvertible.ToString(IFormatProvider? provider)
{{
return ToString(""g"", provider);
return ToString(null, provider);
}}
object IConvertible.ToType(Type conversionType, IFormatProvider? provider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ public void ToString_NullFormat_DefaultsToGeneralFormat(string cultureName)
? null
: CultureInfo.GetCultureInfo(cultureName);
Assert.Equal(quantity.ToString(""g"", formatProvider), quantity.ToString(null, formatProvider));
Assert.Equal(quantity.ToString(""G"", formatProvider), quantity.ToString(null, formatProvider));
}}
[Theory]
Expand Down

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

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

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

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

2 changes: 1 addition & 1 deletion UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs

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

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

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

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

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

2 changes: 1 addition & 1 deletion UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading

0 comments on commit 095c85b

Please sign in to comment.