tplove 发表于 2015-11-28 15:41:19

浮点数怎么转换成字符串数组

如题:浮点数 A= 123.5       怎么变成字符串数组   B = {"1";"2";"3";".";"5"}最好做成子程序模块,谢谢大侠帮忙

小甲鱼 发表于 2015-11-28 16:43:38

// Converts a floating point number to string.
void ftoa(float n, char *res, int afterpoint)
{
    // Extract integer part
    int ipart = (int)n;

    // Extract floating part
    float fpart = n - (float)ipart;

    // convert integer part to string
    int i = intToStr(ipart, res, 0);

    // check for display option after point
    if (afterpoint != 0)
    {
      res = '.';// add dot

      // Get the value of fraction part upto given no.
      // of points after dot. The third parameter is needed
      // to handle cases like 233.007
      fpart = fpart * pow(10, afterpoint);

      intToStr((int)fpart, res + i + 1, afterpoint);
    }
}

迷雾少年 发表于 2015-11-28 20:07:01

本帖最后由 迷雾少年 于 2015-11-28 20:14 编辑

void m_folattoInt( float n, char desMem )
{
        /* find zero */
        int index = 1;

        while ( 1 <= (n / (pow( 10, index ) ) ) )
        {
                index++;
        }

        int l = n * pow( 10.0f, 8 - index );

        itoa( l, desMem, 10 );

        char *p1 = desMem + index + 1, *p2 = desMem + index;

        memcpy( p1, p2, strlen( p1 ) );

        *p2 = '.';
}


已测

迷雾少年 发表于 2015-11-28 20:08:12

小甲鱼 发表于 2015-11-28 16:43


intToStr 是哪个头文件的

tplove 发表于 2015-11-29 13:26:12

谢谢亲们的回复,我爱你们,欧耶

tplove 发表于 2015-12-5 20:37:26

亲们的函数都用到了,不同的头文件,我在单片机C语音中,,利于
整数转换字符串程序
void Int_To_Str(int x,char *Str)
{
        int t;
        char *Ptr,Buf;
        int i = 0;
        Ptr = Str;
        if(x < 10)               
        {
                *Ptr ++ = '0';
                *Ptr ++ = x+0x30;
        }
        else
        {
                while(x > 0)
                {
                        t = x % 10;
                        x = x / 10;
                        Buf = t+0x30;       
                }
                i -- ;
                for(;i >= 0;i --)                
                {
                        *(Ptr++) = Buf;
                }
        }
        *Ptr = '\0';
}

谁能帮我写一个浮点数转换到字符串
void float_to_str(float n, char *str)
{
**************
}

大娘别摸我怕痒 发表于 2015-12-5 20:47:07

顶一下…………
页: [1]
查看完整版本: 浮点数怎么转换成字符串数组