Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Common/Cpp/PrettyPrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scientific notation isn't possible here right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe so. I believe the F format doesn't use scientific notation. But the G format does.

if (str.back() == '.') {
str.pop_back();
}
}
return str;
}



std::string now_to_filestring(){
#if _WIN32 && _MSC_VER
Expand Down
3 changes: 3 additions & 0 deletions Common/Cpp/PrettyPrint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion Common/Qt/Options/FloatingPointWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include "Common/Cpp/PrettyPrint.h"
#include "ConfigWidget.h"
#include "FloatingPointWidget.h"

Expand Down Expand Up @@ -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]{
Expand Down
26 changes: 18 additions & 8 deletions SerialPrograms/Source/CommonFramework/Options/BoxOption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,25 @@ void BoxOption::on_config_value_changed(void* object){
std::string box_coord_string = BOX_COORDINATES;
std::vector<std::string> 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;
Expand Down
9 changes: 5 additions & 4 deletions SerialPrograms/Source/CommonFramework/Options/BoxOption.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
Loading