小伙看你根骨奇佳,潜力无限,来学PHP伐。
Is sprintf not efficient enough? Just take out the sprintf function about %f without using the library function.
If you want to omit the processing of % by sprintf, it is recommended to use the _gcvt_s function.
How to use it:
char buffer[320]; _gcvt_s(buffer, 320, number, 30); vint len = (vint)strlen(buffer); if (buffer[len - 1] == '.') { buffer[len - 1] = 'void _gcvt_s(char* buffer, size_t size, double value, vint numberOfDigits) { sprintf(buffer, "%f", value); char* point = strchr(buffer, '.'); if(!point) return; char* zero = buffer + strlen(buffer); while(zero[-1] == '0') { *--zero = 'rrreee'; } if(zero[-1] == '.') *--zero = 'rrreee'; } '; }
void _gcvt_s(char* buffer, size_t size, double value, vint numberOfDigits) { sprintf(buffer, "%f", value); char* point = strchr(buffer, '.'); if(!point) return; char* zero = buffer + strlen(buffer); while(zero[-1] == '0') { *--zero = 'rrreee'; } if(zero[-1] == '.') *--zero = 'rrreee'; }
If the compiler you are using does not have _gcvt_s, you can encapsulate one yourself:
In this way, the program can exert excellent performance under other compilation periods and under VC++.
Why no one mentioned the grisu algorithm. It is the fastest, "completely correct" algorithm Run in the library: https://github.com/night-shift/fpconv
grisu
Is sprintf not efficient enough?
Just take out the sprintf function about %f without using the library function.
If you want to omit the processing of % by sprintf, it is recommended to use the _gcvt_s function.
How to use it:
If the compiler you are using does not have _gcvt_s, you can encapsulate one yourself:
rrreeeIn this way, the program can exert excellent performance under other compilation periods and under VC++.
Why no one mentioned the
grisu
algorithm. It is the fastest, "completely correct" algorithmRun in the library: https://github.com/night-shift/fpconv