-
Notifications
You must be signed in to change notification settings - Fork 391
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
Comments
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 |
I could also make sense to allow 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. |
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 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: |
Oh, ok, thanks for clarifying that. So if I want to format the temperature in Fahrenheit, I'll have to do |
Correct |
Fixed in #788 |
The documentation of
QuantityFormatter.Format
specifies that (and it also behaves like that):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).
The text was updated successfully, but these errors were encountered: