diff --git a/util/cast.hpp b/util/cast.hpp index 538f8925b..a9e97d6ec 100644 --- a/util/cast.hpp +++ b/util/cast.hpp @@ -33,6 +33,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include +#include + namespace cast { template @@ -47,8 +50,19 @@ template inline std::string to_string_with_preci static_assert(std::is_arithmetic::value, "integral or floating point type required"); std::ostringstream out; - out << std::setprecision(Precision) << x; - return out.str(); + out << std::fixed << std::setprecision(Precision) << x; + auto rv = out.str(); + + // Javascript has no separation of float / int, digits without a '.' are integral typed + // X.Y.0 -> X.Y + // X.0 -> X + boost::trim_right_if(rv, boost::is_any_of("0")); + boost::trim_right_if(rv, boost::is_any_of(".")); + // Note: + // - assumes the locale to use '.' as digit separator + // - this is not identical to: trim_right_if(rv, is_any_of('0 .')) + + return rv; } }