类型转换 - 求一个高效的double转char*的算法,C或C++
伊谢尔伦
伊谢尔伦 2017-04-17 13:10:43
0
3
728
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(3)
大家讲道理

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';
}
'; }

If the compiler you are using does not have _gcvt_s, you can encapsulate one yourself:

rrreee

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template