QbProg
QbProg

Reputation: 1845

fmt : format a float/double without format string

I would like to format a single double/float to a string/wstring with fmt. something like std::string s = fmt:format_??(my_float);

Since the formats I use are kind of fixed, is there a way to use the library without having to use the format string, but directly feeding the settings?

I noticed there are format_int and format_float that uses a settings struct,which may be suitable, but these are inside the detail namespace and also it's not so clear how to initialize the input parameters.

My (unbenchmarked) guess is that avoiding the format string would result in a faster conversion.

Any idea or experience in this regard?

Thanks!

Upvotes: 1

Views: 1536

Answers (1)

vitaut
vitaut

Reputation: 55595

is there a way to use the library without having to use the format string, but directly feeding the settings?

No but if the format is known at compile time you can use format string compilation which completely eliminates the runtime overhead of handling a format string, however small. For example:

std::string s = fmt::format(FMT_COMPILE("{}"), 42);

is the fastest method of converting a number into an std::string (there is also fmt::to_string which is equivalent): https://www.zverovich.net/2020/06/13/fast-int-to-string-revisited.html.

Upvotes: 2

Related Questions