[技术问答] 浮点转字符串的算法

[复制链接]
4498|42
xander0751 发表于 2024-8-17 06:59 | 显示全部楼层
  1. void main()
  2. {
  3.     float value = 3.142;
  4.    
  5.     SendByte((value % 100)/10 + 48);        //输出十位
  6.     SendByte(((uint8)value) % 10 + 48);     //输出个位
  7.     SendByte(0x2E);                                  //输出小数点
  8.     SendByte(((uint16)(value*10)) % 10 + 48);        //输出十分位
  9.     SendByte(((uint16)(value*100)) % 10 + 48);      //输出百分位
  10.     SendByte(((uint16)(value*1000)) % 10 + 48);    //输出千分位
  11. }

  12. void SendByte(uint8 c)              //以51单片机串口为例。根据接收数据的设备和通信方式自定义
  13. {
  14.         SBUF = c;
  15.         while(!TI);                                        //等待数据发送完成
  16.         TI = 0;
  17. }
xander0751 发表于 2024-8-17 07:09 | 显示全部楼层
  1. typedef unsigned char uint8;
  2. typedef unsigned int uint16;

  3. void main()
  4. {
  5.     float value = 3.142;
  6.   
  7.     SendByte(((uint8)value % 100)/10 + 48);                                //输出十位
  8.     SendByte(((uint8)value) % 10 + 48);                                //输出个位
  9.     SendByte(0x2E);                                                                //输出小数点
  10.     SendByte(((uint16)(value*10)) % 10 + 48);        //输出十分位
  11.     SendByte(((uint16)(value*100)) % 10 + 48);        //输出百分位
  12.     SendByte(((uint16)(value*1000)) % 10 + 48);        //输出千分位
  13. }

  14. void SendByte(uint8 c)        //以51单片机为例。不同接收设备或通信方式可自定义
  15. {
  16.         SBUF = c;                                  //存入发送缓冲,移位寄存器将自动发送
  17.         while(!TI);                                        //等待数据发送完成
  18.         TI = 0;
  19. }
xander0751 发表于 2024-8-17 07:10 | 显示全部楼层
  1. typedef unsigned char uint8;
  2. typedef unsigned int uint16;

  3. void main()
  4. {
  5.     float value = 3.142;
  6.   
  7.     SendByte(((uint8)value % 100)/10 + 48);                                //输出十位
  8.     SendByte(((uint8)value) % 10 + 48);                                //输出个位
  9.     SendByte(0x2E);                                                                //输出小数点
  10.     SendByte(((uint16)(value*10)) % 10 + 48);        //输出十分位
  11.     SendByte(((uint16)(value*100)) % 10 + 48);        //输出百分位
  12.     SendByte(((uint16)(value*1000)) % 10 + 48);        //输出千分位
  13. }

  14. void SendByte(uint8 c)        //以51单片机为例。不同接收设备或通信方式可自定义
  15. {
  16.         SBUF = c;                                  //存入发送缓冲,移位寄存器将自动发送
  17.         while(!TI);                                        //等待数据发送完成
  18.         TI = 0;
  19. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 在线客服 返回列表 返回顶部