YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Option to specify custom format for floating numbers

Open n9 opened this issue 6 months ago • 1 comments

Is your feature request related to a problem? Please describe.

It is related to https://github.com/aaubry/YamlDotNet/issues/637. That issue was solved by changing the format from G17 to G in https://github.com/aaubry/YamlDotNet/pull/695.

But, numbers like 0.1 * 3 are still serialized as 0.30000000000000004. This is correct, but in case the YAML is used by humans, it is not user friendly.

Describe the solution you'd like

It would be great to have an option for a custom "precision" (to be able to set e.g. G16).

Describe alternatives you've considered

A workaround is "manual" rounding of the number to the required precision before serializing.

Additional context

None.

n9 avatar Jul 03 '25 08:07 n9

You can use the YamlFormatter which is the number/value formatter used to set that information. It should solve your needs.

EdwardCooke avatar Sep 16 '25 20:09 EdwardCooke