question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

String formatting of Temperature, Pressure and future physical quantities

See original GitHub issue

While 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.

CC: @MarkCiliaVincenti @krwq

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:27 (27 by maintainers)

github_iconTop GitHub Comments

1reaction
pgrawehrcommented, May 23, 2020

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).

1reaction
pgrawehrcommented, Apr 21, 2020

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).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Units and Quantities (astropy.units)
units handles defining, converting between, and performing arithmetic with physical quantities. Examples of physical quantities are meters, seconds, Hz, etc.
Read more >
String formatting specification — pint 0.22 documentation
The conversion of Unit and Quantity objects to strings (e.g. through the str builtin or f-strings) can be customized using format specifications.
Read more >
standard hydrometeorological exchange format (shef) code ...
In SHEF messages, different types of data are keyed by a seven- character parameter code represented by the character string “PEDTSEP.” The ...
Read more >
Format Specifier Syntax
You use format specifiers to format strings , convert a number into a string , and insert non-displayable ... These are examples of...
Read more >
How to format numbers, dates, enums, and other types in . ...
Standard format strings for numeric types usually define a result string whose precise appearance is controlled by one or more property values.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found