Skip to content

Quantity formatting: Trailing zeros #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pgrawehr opened this issue May 21, 2020 · 6 comments · Fixed by #788
Closed

Quantity formatting: Trailing zeros #786

pgrawehr opened this issue May 21, 2020 · 6 comments · Fixed by #788

Comments

@pgrawehr
Copy link
Contributor

The documentation of QuantityFormatter.Format specifies that (and it also behaves like that):

/// "s1", "s2", ..., "sN": The value with N significant digits after the radix followed by the unit abbreviation. For example,
/// "s4" would return "1.2345 m" if <see cref="IQuantity.Value" /> is 1.2345678. Trailing zeros are omitted.

Is there a reason trailing zeros are ommitted? Besides that this constantly changes the length of the output string (if the value changes slightly), it may present a value different from the expectation. For instance, if I have a temperature sensor from which I know it is accurate to 0.01 °C, I would want to use "s2" to print its value with two digits. The output now prints either "20.02 °C" or "20 °C", just because the measurement has slightly changed. Reading the later, I don't know that the measurement was actually "20.00 °C".

For a scientist, it does make a difference whether a reading says "20 °C" or "20.00 °C". I'd like to see an option to force the number of digits to be fixed (much like the "F" default number format specifier does).

@tmilnthorp
Copy link
Collaborator

tmilnthorp commented May 22, 2020

For the most flexibility, you will want to use the built-in string formatting. For your case, I would suggest something as follows:

var temperature = UnitsNet.Temperature.FromDegreesCelsius( 20.02 );
var output = string.Format( "{0:F3} {1:a}", temperature.Value, temperature );

This will result in the string "20.020 °C"

@angularsen we could do is support all the standard format strings. If we encounter one of them, apply them only to the value portion of the string. Mercifully, the only overlap we have is the "g" format. Taking the example from above:

var temperature = UnitsNet.Temperature.FromDegreesCelsius( 20.02 );
var output = string.Format( "{0:F3} {0:a}", temperature );

For custom formats, you would still have to use string.Format per the first example.

@pgrawehr
Copy link
Contributor Author

I could also make sense to allow string.Format( "{0:F3 a}", temperature ); That would allow for maximum flexibility. Idealy, string.Format( "{0:F3 a2}", temperature ); would then return a converted value (72.223 °F), since the value in celcius with another unit name is meaningless.

Supporting all the standard format strings would be a good idea. The overlap with the "g" isn't so big of a deal I think, since "G" is still available and (unless you are into astrophysics, maybe) we usually use the unit that best suits the number, so that scientific notation is not needed.

@tmilnthorp
Copy link
Collaborator

I agree, we just don't support compound format strings currently. I created #788 to get us a bit closer to what you need.

FYI "{0:a2}" would not give °F for a °C value. It's only for when there multiple abbreviations for a unit. This will throw a FormatException:

var temperature = UnitsNet.Temperature.FromDegreesCelsius( 20.02 );
var output = string.Format( "{0:a2}", temperature );

An example use for rotational speed:

var rotationalSpeed = RotationalSpeed.FromDegreesPerMinute( 123.456 );
var output = string.Format( "{0:a0} OR {0:a1}", rotationalSpeed );

Output: °/min OR deg/min

@pgrawehr
Copy link
Contributor Author

Oh, ok, thanks for clarifying that. So if I want to format the temperature in Fahrenheit, I'll have to do temperature.ToUnit(TemperatureUnit.DegreeFahrenheit).ToString("s2"} or similar.

@tmilnthorp
Copy link
Collaborator

Correct

@angularsen
Copy link
Owner

angularsen commented May 28, 2020

Fixed in #788

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants