Skip to content

Commit

Permalink
Merge pull request #4112 from AI0867/si_string-default-format
Browse files Browse the repository at this point in the history
Restore 3-significant-digit si_string (closes #3871)
  • Loading branch information
AI0867 committed Jun 21, 2019
2 parents 4830028 + 4141602 commit c0191a5
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/serialization/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,30 @@ std::string half_signed_value(int val)

static void si_string_impl_stream_write(std::stringstream &ss, double input) {
std::streamsize oldprec = ss.precision();
#ifdef _MSC_VER
// For MSVC, default mode misbehaves, so we use fixed instead.
ss.precision(1);
ss << std::fixed
<< input;
#else
// In default mode, precision sets the number of significant figures.

// 999.5 and above will render as 1000+, however, only numbers above 1000 will use 4 digits
// Rounding everything from 100 up (at which point we stop using decimals anyway) avoids this.
if (input >= 100) {
input = std::round(input);
}

// When in binary mode, numbers of up to 1023.9999 can be passed
// We should render those with 4 digits, instead of as 1e+3.
// Input should be an integer number now, but doubles can do strange things, so check the halfway point instead.
if (input >= 999.5) {
ss.precision(4);
} else {
ss.precision(3);
}
ss << input;
#endif
ss.precision(oldprec);
}

Expand Down

0 comments on commit c0191a5

Please sign in to comment.