Merge pull request #4112 from AI0867/si_string-default-format
Restore 3-significant-digit si_string (closes #3871)
This commit is contained in:
commit
c0191a5a16
1 changed files with 21 additions and 0 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue