String formatting of Temperature, Pressure and future physical quantities
See original GitHub issueWhile working on the WeatherHelper stuff, I found that it was pretty inconvenient that the Temperature
struct did not have a ToString()
member. So I added one, and also added a public string ToString(string formatArgs, IFormatProvider culture)
method, that takes formatting arguments to print Temperature in different units.
With PR #1046 it is now proposed to do the same for Pressure, but there are a lot of possible ways one may want to format such values, so we need a general concept on how to create such formatting members and what kind of formatting options should be supported.
As a reference, the two proposed ToString() functions for Temperature and Pressure:
/// <summary>
/// Returns the string representation of this pressure, with the given format string and using the given culture.
/// Valid format specifiers are:
/// PA: Pascal
/// MBAR: Millibar
/// KPA: Kilopascal
/// HPA: Hectopascal
/// INHG: Inch of mercury
/// MMHG: Millimeter of mercury
/// An extra number defines the number of decimal digits to use (default 1)
/// <example>
/// <code>
/// var s = p.ToString("PA2"); -> "101325.01 Pa"
/// var s = p.ToString("HPA2"); --> "1013.25 hPa"
/// </code>
/// </example>
/// </summary>
/// <param name="formatArgs">Format string</param>
/// <param name="culture">Culture to use. Affects the format of the number.</param>
/// <returns>String representation</returns>
public string ToString(string formatArgs, IFormatProvider culture)
/// <summary>
/// Returns the string representation of this temperature, with the given format string and using the given culture.
/// Valid format specifiers are:
/// C: Degrees celsius
/// F: Degrees Fahrenheit
/// K: Degrees Kelvin
/// An extra number defines the number of decimal digits to use (default 1)
/// <example>
/// <code>
/// var s = t.ToString("K2"); -> "293.36°K"
/// var s = t.ToString("C"); --> "20.21°C"
/// </code>
/// </example>
/// </summary>
/// <param name="formatArgs">Format string</param>
/// <param name="culture">Culture to use. Affects the format of the number.</param>
/// <returns>String representation</returns>
public string ToString(string formatArgs, IFormatProvider culture)
Questions include:
- Should such formatting members generally be included or not?
- How would one specify the unit?
- Do we need to make the space between unit and number changeable?
- May the unit name be culture-dependent? (I don’t know how a temperature is written in China)
I guess that including a standard formatting for the parameterless ToString()
is less disputed and should be done in either case.
Issue Analytics
- State:
- Created 3 years ago
- Comments:27 (27 by maintainers)
Top GitHub Comments
Closing this, as the move to UnitsNet removes the burden of the formatting problem from our library. Opened https://github.com/angularsen/UnitsNet/issues/786 and https://github.com/angularsen/UnitsNet/issues/787 for some findings over there (maybe more to come, as we start using it).
What krwq means: For some quantities, the default unit is clear. For temperature, it’s Fahrenheit in the US and Celsius everywhere else. But for others it’s not so clear: For pressure, it depends on context whether you want mmHG/inchHG or Bar/Pascal/Hectopascal. Or for speed, it might normally be Kilometers/hour vs Miles/hour, except if you are into aviatics or shipping, where it would be knots. And for physicans, it’s meters/second (which is the SI unit for it).