Format localized float


I need to generate data for an application which only accept localized data, and refuse a float using a dot as separator.

I can write a function like this one:

let format_float
  : float -> string
  = fun f -> 
    String.concat "," (String.split_on_char '.' (Float.to_string f))

but I would like if there is a simple way to do the same, either with a dedicated library, or with printf trick that I’m not aware of. Do you have a nice solution for that ?

Thanks !

I don’t think there is anything available in the stdlib for this. In fact, the stdlib makes some effort to make sure the output is locale independent (see eg make string_of_float and float_of_string locale-independent by ygrek · Pull Request #1803 · ocaml/ocaml · GitHub).


1 Like