鱼C论坛

 找回密码
 立即注册
查看: 3934|回复: 6

[已解决]浮点数怎么转换成字符串数组

[复制链接]
发表于 2015-11-28 15:41:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如题:浮点数 A  = 123.5       怎么变成字符串数组   B[5] = {"1";"2";"3";".";"5"}  最好做成子程序模块,谢谢大侠帮忙
最佳答案
2015-11-28 16:43:38
  1. // Converts a floating point number to string.
  2. void ftoa(float n, char *res, int afterpoint)
  3. {
  4.     // Extract integer part
  5.     int ipart = (int)n;

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

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

  10.     // check for display option after point
  11.     if (afterpoint != 0)
  12.     {
  13.         res[i] = '.';  // add dot

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

  18.         intToStr((int)fpart, res + i + 1, afterpoint);
  19.     }
  20. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-11-28 16:43:38 | 显示全部楼层    本楼为最佳答案   
  1. // Converts a floating point number to string.
  2. void ftoa(float n, char *res, int afterpoint)
  3. {
  4.     // Extract integer part
  5.     int ipart = (int)n;

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

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

  10.     // check for display option after point
  11.     if (afterpoint != 0)
  12.     {
  13.         res[i] = '.';  // add dot

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

  18.         intToStr((int)fpart, res + i + 1, afterpoint);
  19.     }
  20. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-11-28 20:07:01 | 显示全部楼层
本帖最后由 迷雾少年 于 2015-11-28 20:14 编辑
  1. void m_folattoInt( float n, char desMem[20] )
  2. {
  3.         /* find zero */
  4.         int index = 1;

  5.         while ( 1 <= (n / (pow( 10, index ) ) ) )
  6.         {
  7.                 index++;
  8.         }

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

  10.         itoa( l, desMem, 10 );

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

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

  13.         *p2 = '.';
  14. }
复制代码


已测
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-11-28 20:08:12 | 显示全部楼层

intToStr 是哪个头文件的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-11-29 13:26:12 | 显示全部楼层
谢谢亲们的回复,我爱你们,欧耶
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-5 20:37:26 | 显示全部楼层
亲们的函数都用到了,不同的头文件,我在单片机C语音中,,利于
整数转换字符串  程序
void Int_To_Str(int x,char *Str)
{
        int t;
        char *Ptr,Buf[5];
        int i = 0;
        Ptr = Str;
        if(x < 10)               
        {
                *Ptr ++ = '0';
                *Ptr ++ = x+0x30;
        }
        else
        {
                while(x > 0)
                {
                        t = x % 10;
                        x = x / 10;
                        Buf[i++] = t+0x30;       
                }
                i -- ;
                for(;i >= 0;i --)                
                {
                        *(Ptr++) = Buf[i];
                }
        }
        *Ptr = '\0';
}

谁能帮我写一个  浮点数转换到字符串
void float_to_str(float n, char *str)
{
**************
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-12-5 20:47:07 | 显示全部楼层
顶一下…………
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 14:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表