Skip to content

Commit

Permalink
Fix ToString for doubles and floats
Browse files Browse the repository at this point in the history
- The output was wrong when specifying a number of decimal places

Signed-off-by: José Simões <[email protected]>
  • Loading branch information
josesimoes committed Aug 8, 2018
1 parent 279df1b commit b5fa10b
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/CLR/CorLib/corlib_native_System_Number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include "CorLib.h"

// compose sprintf format string according to requested parameters
void nf_GetFormatString(char* formatStr, char formatCh, int precision, bool isLong, bool isSigned)
void nf_GetFormatString(char* formatStr, char formatCh, int precision, bool isLong, bool isFloat, bool isSigned)
{
sprintf(formatStr, "%%%s0%d%s%s", isSigned ? "-" : "", precision, isLong ? "l" : "", formatCh == 'X' ? "X" : isSigned ? "d" : "u");
sprintf(formatStr, "%%%s%d%s%s", isFloat ? "." : isSigned ? "-0" : "0", precision, isLong ? "l" : isFloat ? "f" : "", formatCh == 'X' ? "X" : isFloat ? "" : isSigned ? "d" : "u");
}

void nf_RemoveTrailingZeros(char* buffer, int32_t length)
Expand Down Expand Up @@ -61,37 +61,37 @@ HRESULT Library_corlib_native_System_Number::FormatNative___STATIC__STRING__OBJE
{
case DATATYPE_I1:
// get format string
nf_GetFormatString(formatStr, formatCh, precision, false, true);
nf_GetFormatString(formatStr, formatCh, precision, false, false, true);
sprintf(result, formatStr, value->NumericByRef().s1 );
break;

case DATATYPE_U1:
// get format string
nf_GetFormatString(formatStr, formatCh, precision, false, false);
nf_GetFormatString(formatStr, formatCh, precision, false, false, false);
sprintf(result, formatStr, value->NumericByRef().u1 );
break;

case DATATYPE_I2:
// get format string
nf_GetFormatString(formatStr, formatCh, precision, false, true);
nf_GetFormatString(formatStr, formatCh, precision, false, false, true);
sprintf(result, formatStr, value->NumericByRef().s2 );
break;

case DATATYPE_U2:
// get format string
nf_GetFormatString(formatStr, formatCh, precision, false, false);
nf_GetFormatString(formatStr, formatCh, precision, false, false, false);
sprintf(result, formatStr, value->NumericByRef().u2 );
break;

case DATATYPE_I4:
// get format string
nf_GetFormatString(formatStr, formatCh, precision, false, true);
nf_GetFormatString(formatStr, formatCh, precision, false, false, true);
sprintf(result, formatStr, value->NumericByRef().s4);
break;

case DATATYPE_U4:
// get format string
nf_GetFormatString(formatStr, formatCh, precision, false, false);
nf_GetFormatString(formatStr, formatCh, precision, false, false, false);
sprintf(result, formatStr, value->NumericByRef().u4);
break;

Expand All @@ -101,7 +101,7 @@ HRESULT Library_corlib_native_System_Number::FormatNative___STATIC__STRING__OBJE
hal_snprintf( result, ARRAYSIZE(result), "%I64d",(CLR_INT64_TEMP_CAST)value->NumericByRef().s8 );
#else
// get format string
nf_GetFormatString(formatStr, formatCh, precision, true, true);
nf_GetFormatString(formatStr, formatCh, precision, true, false, true);
sprintf(result, formatStr, (CLR_INT64_TEMP_CAST)value->NumericByRef().s8);
#endif // defined(_WIN32)
}
Expand All @@ -113,7 +113,7 @@ HRESULT Library_corlib_native_System_Number::FormatNative___STATIC__STRING__OBJE
hal_snprintf( result, ARRAYSIZE(result), "%I64u",(CLR_UINT64_TEMP_CAST)value->NumericByRef().u8 );
#else
// get format string
nf_GetFormatString(formatStr, formatCh, precision, true, false);
nf_GetFormatString(formatStr, formatCh, precision, true, false, false);
sprintf(result, formatStr, (CLR_UINT64_TEMP_CAST)value->NumericByRef().u8 );
#endif // defined(_WIN32)
}
Expand All @@ -136,7 +136,13 @@ HRESULT Library_corlib_native_System_Number::FormatNative___STATIC__STRING__OBJE
#else

#if !defined(NANOCLR_EMULATED_FLOATINGPOINT)
sprintf(result, "%.9f", value->NumericByRef().r4);
// use default precision is none is specfied AND if format in not generic
precision = (formatCh == 'G' && precision == 1) ? 9 : precision;

// get format string
nf_GetFormatString(formatStr, formatCh, precision, false, true, false);

sprintf(result, formatStr, value->NumericByRef().r4);
nf_RemoveTrailingZeros(result, ARRAYSIZE(result));
#else
CLR_INT32 f = value->NumericByRef().r4;
Expand All @@ -162,7 +168,13 @@ HRESULT Library_corlib_native_System_Number::FormatNative___STATIC__STRING__OBJE
hal_snprintf( result, ARRAYSIZE(result), "%.15g", (CLR_DOUBLE_TEMP_CAST)value->NumericByRef().r8 );
#else
#if !defined(NANOCLR_EMULATED_FLOATINGPOINT)
sprintf(result, "%.9f", (CLR_DOUBLE_TEMP_CAST)value->NumericByRef().r8);
// use default precision is none is specfied AND format is not generic
precision = (formatCh == 'G' && precision == 1) ? 15 : precision;

// get format string
nf_GetFormatString(formatStr, formatCh, precision, false, true, false);

sprintf(result, formatStr, (CLR_DOUBLE_TEMP_CAST)value->NumericByRef().r8);
nf_RemoveTrailingZeros(result, ARRAYSIZE(result));
#else
CLR_INT64 d = (CLR_DOUBLE_TEMP_CAST)value->NumericByRef().r8;
Expand Down

0 comments on commit b5fa10b

Please sign in to comment.