{"action":"created","issue":{"url":"https://api.github.com/repos/fmtlib/fmt/issues/331","repository_url":"https://api.github.com/repos/fmtlib/fmt","labels_url":"https://api.github.com/repos/fmtlib/fmt/issues/331/labels{/name}","comments_url":"https://api.github.com/repos/fmtlib/fmt/issues/331/comments","events_url":"https://api.github.com/repos/fmtlib/fmt/issues/331/events","html_url":"https://github.com/fmtlib/fmt/issues/331","id":155695470,"number":331,"title":"Desired feature:  ability to disable sign printing on floating point zeros. (custom formatting)","user":{"login":"spacemoose","id":6187802,"avatar_url":"https://avatars.githubusercontent.com/u/6187802?v=3","gravatar_id":"","url":"https://api.github.com/users/spacemoose","html_url":"https://github.com/spacemoose","followers_url":"https://api.github.com/users/spacemoose/followers","following_url":"https://api.github.com/users/spacemoose/following{/other_user}","gists_url":"https://api.github.com/users/spacemoose/gists{/gist_id}","starred_url":"https://api.github.com/users/spacemoose/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spacemoose/subscriptions","organizations_url":"https://api.github.com/users/spacemoose/orgs","repos_url":"https://api.github.com/users/spacemoose/repos","events_url":"https://api.github.com/users/spacemoose/events{/privacy}","received_events_url":"https://api.github.com/users/spacemoose/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-05-19T09:45:18Z","updated_at":"2016-05-19T14:33:18Z","closed_at":null,"body":"We recently experimented with using fmtlib to alleviate a performance problem we were having with boost::format.  I'm quite impressed with the library, but I have a feature need that prevents me from using it as is.\r\n\r\nWe need the ability to NOT print a sign when a floating point representation is textually zero.  For example you have a number which is zero, but because of floating point precision issues, it's represented in memory is -0.000001.   When we print \"0.00\" we get an unwanted sign -- \"-0.00\".  We were able to solve this for boost::format by creating a custom num_put facet, but that approach won't work for fmtlib.\r\n\r\nWe can patch the library to get it to behave the way we want, but I suppose the reasons why this is undesirable is obvious.\r\n\r\nSo -- would you be interested in a patch that adds this functionality?  Obviously we would first have to discuss the interface for controlling such a feature.  I have some thoughts regarding the ability to add custom formatters to fmtlib similar to std::locale's facet functionality,  but perhaps you already have thoughts on this topic   If you think this might be interesting, let's start a dialogue. "},"comment":{"url":"https://api.github.com/repos/fmtlib/fmt/issues/comments/220342752","html_url":"https://github.com/fmtlib/fmt/issues/331#issuecomment-220342752","issue_url":"https://api.github.com/repos/fmtlib/fmt/issues/331","id":220342752,"user":{"login":"vitaut","id":576385,"avatar_url":"https://avatars.githubusercontent.com/u/576385?v=3","gravatar_id":"","url":"https://api.github.com/users/vitaut","html_url":"https://github.com/vitaut","followers_url":"https://api.github.com/users/vitaut/followers","following_url":"https://api.github.com/users/vitaut/following{/other_user}","gists_url":"https://api.github.com/users/vitaut/gists{/gist_id}","starred_url":"https://api.github.com/users/vitaut/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vitaut/subscriptions","organizations_url":"https://api.github.com/users/vitaut/orgs","repos_url":"https://api.github.com/users/vitaut/repos","events_url":"https://api.github.com/users/vitaut/events{/privacy}","received_events_url":"https://api.github.com/users/vitaut/received_events","type":"User","site_admin":false},"created_at":"2016-05-19T14:33:18Z","updated_at":"2016-05-19T14:33:18Z","body":"Thanks for the suggestion and your interest in the library.\r\n\r\nYou can achieve the desired behavior by providing a [custom argument formatter](http://fmtlib.net/latest/api.html), for example:\r\n\r\n```c++\r\n#include \"fmt/format.h\"\r\n\r\n// A custom argument formatter that doesn't print `-` for floating-point values\r\n// rounded to 0.\r\nclass CustomArgFormatter :\r\n  public fmt::BasicArgFormatter<CustomArgFormatter, char>  {\r\n  public:\r\n  CustomArgFormatter(fmt::BasicFormatter<char, CustomArgFormatter> &f,\r\n                     fmt::FormatSpec &s, const char *fmt)\r\n    : fmt::BasicArgFormatter<CustomArgFormatter, char>(f, s, fmt) {}\r\n\r\n  void visit_double(double value) {\r\n    if (round(value * pow(10, spec().precision())) == 0)\r\n      value = 0;\r\n    fmt::BasicArgFormatter<CustomArgFormatter, char>::visit_double(value);\r\n  }\r\n};\r\n\r\nstd::string custom_format(const char *format_str, fmt::ArgList args) {\r\n  fmt::MemoryWriter writer;\r\n  // Pass custom argument formatter as a template arg to BasicFormatter.\r\n  fmt::BasicFormatter<char, CustomArgFormatter> formatter(args, writer);\r\n  formatter.format(format_str);\r\n  return writer.str();\r\n}\r\nFMT_VARIADIC(std::string, custom_format, const char *)\r\n\r\nint main() {\r\n  custom_format(\"{:.2f}\", -0.000001); // returns \"0.00\"\r\n}\r\n```\r\n\r\nWill it work for you?\r\n"}}