Use {fmt} for double -> string conversions

Faster at printing floats than sprintf due to the modern algos
This commit is contained in:
Marek Roszko
2022-09-13 23:17:15 -04:00
parent f304e2d4f6
commit 03fff6e58d
2 changed files with 23 additions and 23 deletions
+13 -14
View File
@@ -31,6 +31,7 @@
#include <macros.h>
#include <richio.h> // StrPrintf
#include <string_utils.h>
#include <fmt/core.h>
/**
@@ -1090,29 +1091,27 @@ void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed )
std::string Double2Str( double aValue )
{
char buf[50];
std::string buf;
int len;
if( aValue != 0.0 && std::fabs( aValue ) <= 0.0001 )
{
// For these small values, %f works fine,
// and %g gives an exponent
len = sprintf( buf, "%.16f", aValue );
// For these small values, 'f' works fine,
// and 'g' can result in exponent notation
buf = fmt::format( "{:.16f}", aValue );
while( --len > 0 && buf[len] == '0' )
buf[len] = '\0';
if( buf[len] == '.' )
buf[len] = '\0';
else
++len;
// remove trailing zeros
while( !buf.empty() && buf[buf.size() - 1] == '0' )
{
buf.pop_back();
}
}
else
{
// For these values, %g works fine, and sometimes %f
// For these values, 'g' works fine, and sometimes 'f'
// gives a bad value (try aValue = 1.222222222222, with %.16f format!)
len = sprintf( buf, "%.10g", aValue );
buf = fmt::format( "{:.10g}", aValue );
}
return std::string( buf, len );
return buf;
}