Skip to content

Controling the precision of float conversion in Yaml.to_string #79

@ferdv

Description

@ferdv

Currently, when serializing a `Float value using to_string, the Printf.sprintf is used with the format hard-coded to "%.16g":

| `Float s -> Stream.scalar (scalar (Printf.sprintf "%.16g" s)) t

In practice this results in the following:

# let x = 94.96 in
  string_of_float x,
  Printf.sprintf "%0.15g" x,
  Printf.sprintf "%0.16g" x,
  Yaml.to_string (`Float x);;
- : string * string * string * string Yaml.res =
("94.96", "94.96", "94.95999999999999", Ok "94.95999999999999\n")

Would it be possible introduce a way to control the float format precision? Alternatively, I propose hard-coding "%0.15g" would work better in terms of readability.

Note: of_json uses Stdlib.string_of_float, which itself uses "%0.12g" as the format.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions