diff --git a/Common/Cpp/PrettyPrint.cpp b/Common/Cpp/PrettyPrint.cpp index 0deb688a62..b133e5e066 100644 --- a/Common/Cpp/PrettyPrint.cpp +++ b/Common/Cpp/PrettyPrint.cpp @@ -174,6 +174,18 @@ std::string tostr_fixed(double x, int precision){ return std::format("{:.{}f}", x, precision); } +std::string tostr_fixed_no_trailing_zero(double x, int precision){ + std::string str = std::format("{:.{}f}", x, precision); + if (str.find('.') != std::string::npos) { + str.erase(str.find_last_not_of('0') + 1, std::string::npos); + if (str.back() == '.') { + str.pop_back(); + } + } + return str; +} + + std::string now_to_filestring(){ #if _WIN32 && _MSC_VER diff --git a/Common/Cpp/PrettyPrint.h b/Common/Cpp/PrettyPrint.h index 5d538765b0..3f83542883 100644 --- a/Common/Cpp/PrettyPrint.h +++ b/Common/Cpp/PrettyPrint.h @@ -23,6 +23,9 @@ std::string tostr_default(double x); // The precision specifies the number of digits after the decimal point. std::string tostr_fixed(double x, int precision); +// Convert double to string with fixed precision, without trailing zeros. +std::string tostr_fixed_no_trailing_zero(double x, int precision); + // Format current time to a string to be used as filenames. // e.g. "20220320-044444408355" std::string now_to_filestring(); diff --git a/Common/Qt/Options/FloatingPointWidget.cpp b/Common/Qt/Options/FloatingPointWidget.cpp index 9ffa190ff1..3413e94196 100644 --- a/Common/Qt/Options/FloatingPointWidget.cpp +++ b/Common/Qt/Options/FloatingPointWidget.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "Common/Cpp/PrettyPrint.h" #include "ConfigWidget.h" #include "FloatingPointWidget.h" @@ -69,7 +70,7 @@ FloatingPointCellWidget::FloatingPointCellWidget(QWidget& parent, FloatingPointC value.add_listener(*this); } void FloatingPointCellWidget::update_value(){ - this->setText(QString::number(m_value, 'f')); + this->setText(QString::fromStdString(tostr_fixed_no_trailing_zero(m_value, 6))); } void FloatingPointCellWidget::on_config_value_changed(void* object){ QMetaObject::invokeMethod(this, [this]{ diff --git a/SerialPrograms/Source/CommonFramework/Options/BoxOption.cpp b/SerialPrograms/Source/CommonFramework/Options/BoxOption.cpp index ba841f5a11..11f8530228 100644 --- a/SerialPrograms/Source/CommonFramework/Options/BoxOption.cpp +++ b/SerialPrograms/Source/CommonFramework/Options/BoxOption.cpp @@ -65,15 +65,25 @@ void BoxOption::on_config_value_changed(void* object){ std::string box_coord_string = BOX_COORDINATES; std::vector all_coords = StringTools::split(box_coord_string, ", "); - std::string x_string = all_coords[0]; - std::string y_string = all_coords[1]; - std::string width_string = all_coords[2]; - std::string height_string = all_coords[3]; + double x_coord = 0.0; + double y_coord = 0.0; + double width_coord = 0.0; + double height_coord = 0.0; - double x_coord = std::stod(x_string); - double y_coord = std::stod(y_string); - double width_coord = std::stod(width_string); - double height_coord = std::stod(height_string); + double* targets[] = { &x_coord, &y_coord, &width_coord, &height_coord }; + size_t limit = std::min(all_coords.size(), size_t(4)); + + for (size_t i = 0; i < limit; ++i) { + const auto& s = all_coords[i]; + char* endptr; + // Parses directly from the buffer without locale or exception overhead + double value = std::strtod(s.data(), &endptr); + + // Validate that at least some characters were parsed successfully + if (endptr != s.data()) { + *targets[i] = value; + } + } // cout << box_coord_string << endl; // cout << std::to_string(x_coord) << endl; diff --git a/SerialPrograms/Source/CommonFramework/Options/BoxOption.h b/SerialPrograms/Source/CommonFramework/Options/BoxOption.h index fdf87897c7..eaf8120c04 100644 --- a/SerialPrograms/Source/CommonFramework/Options/BoxOption.h +++ b/SerialPrograms/Source/CommonFramework/Options/BoxOption.h @@ -7,6 +7,7 @@ #ifndef PokemonAutomation_BoxOption_H #define PokemonAutomation_BoxOption_H +#include "Common/Cpp/PrettyPrint.h" #include "Common/Cpp/Options/GroupOption.h" #include "Common/Cpp/Options/FloatingPointOption.h" #include "Common/Cpp/Options/StringOption.h" @@ -38,10 +39,10 @@ class BoxOption : public GroupOption, public ConfigOption::Listener{ private: std::string make_full_str() const{ return - std::to_string(X) + ", " + - std::to_string(Y) + ", " + - std::to_string(WIDTH) + ", " + - std::to_string(HEIGHT); + tostr_fixed_no_trailing_zero(double(X), 6) + ", " + + tostr_fixed_no_trailing_zero(double(Y), 6) + ", " + + tostr_fixed_no_trailing_zero(double(WIDTH), 6) + ", " + + tostr_fixed_no_trailing_zero(double(HEIGHT), 6); } public: