From 0c92076a142a9700b1bdbfcf8c1104b9ce05bfe0 Mon Sep 17 00:00:00 2001 From: supermerill Date: Tue, 4 Jan 2022 18:38:21 +0100 Subject: [PATCH] Fix to_string_nozero formatter when rounded without any '.'. supermerill/SuperSlicer#2172 --- src/libslic3r/GCodeWriter.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/libslic3r/GCodeWriter.cpp b/src/libslic3r/GCodeWriter.cpp index 20c24982f..d13ddf1bc 100644 --- a/src/libslic3r/GCodeWriter.cpp +++ b/src/libslic3r/GCodeWriter.cpp @@ -35,16 +35,18 @@ std::string to_string_nozero(double value, int32_t max_precision) { ss << std::fixed << std::setprecision(int(std::min(15 - long10, int(max_precision)))) << value; std::string ret = ss.str(); uint8_t nb_del = 0; - uint8_t idx_char; - for (idx_char = uint8_t(ss.tellp()) - 1; idx_char > 0; idx_char--) { - if (ret[idx_char] == '0') + if (ret.find('.') != std::string::npos) { + uint8_t idx_char; + for (idx_char = uint8_t(ss.tellp()) - 1; idx_char > 0; idx_char--) { + if (ret[idx_char] == '0') + nb_del++; + else + break; + } + // remove the '.' at the end of the int + if (idx_char > 0 && ret[idx_char] == '.') nb_del++; - else - break; } - // remove the '.' at the end of the int - if(idx_char > 0 && ret[idx_char] == '.') - nb_del++; if (nb_del > 0) return ret.substr(0, ret.size() - nb_del);