Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prettier floating point numbers #1293

Merged
merged 12 commits into from
Nov 7, 2024
3 changes: 2 additions & 1 deletion include/yaml-cpp/emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "yaml-cpp/emittermanip.h"
#include "yaml-cpp/null.h"
#include "yaml-cpp/ostream_wrapper.h"
#include "yaml-cpp/fptostring.h"

namespace YAML {
class Binary;
Expand Down Expand Up @@ -180,7 +181,7 @@ inline Emitter& Emitter::WriteStreamable(T value) {
}

if (!special) {
stream << value;
stream << FpToString(value, stream.precision());
}
m_stream << stream.str();

Expand Down
15 changes: 15 additions & 0 deletions include/yaml-cpp/fptostring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef YAML_H_FPTOSTRING
#define YAML_H_FPTOSTRING

#include "yaml-cpp/dll.h"

#include <string>

namespace YAML {
// "precision = 0" refers to shortest known unique representation of the value
YAML_CPP_API std::string FpToString(float v, size_t precision = 0);
YAML_CPP_API std::string FpToString(double v, size_t precision = 0);
YAML_CPP_API std::string FpToString(long double v, size_t precision = 0);
}

#endif
3 changes: 2 additions & 1 deletion include/yaml-cpp/node/convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "yaml-cpp/node/node.h"
#include "yaml-cpp/node/type.h"
#include "yaml-cpp/null.h"
#include "yaml-cpp/fptostring.h"


namespace YAML {
Expand Down Expand Up @@ -129,7 +130,7 @@ inner_encode(const T& rhs, std::stringstream& stream){
stream << ".inf";
}
} else {
stream << rhs;
stream << FpToString(rhs, stream.precision());
}
}

Expand Down
Loading
Loading