浮点数在串口打印的函数
来源:互联网 发布:sql字符串函数 upper 编辑:程序博客网 时间:2024/05/29 17:19
/********************************************************************************************************
网上的浮点数在串口打印的函数都不太完美,于是参考他们的一些算法自己写了一个比较完善的算法
可以完成包括负数和正数的浮点数转字符串的转换
****************************************************************************************************/
#include <stdio.h>
typedef unsigned char uint8;
typedef unsigned int uint32;
uint8 Float2Char(float value,uint8 *array);
void main()
{
uint8 A[10];
float value= -123.456789;
uint8 i;
Float2Char(value,A);
printf("%s",A);
}
uint8 Float2Char(float value,uint8 *array)
{
float DecimalPart;
uint32 IntegerPart;
uint8 i = 0, j = 0, temp;
//分离整数和小数
if(value < 0)
{
value = -value ;
array[i++] = '-';
}
else
array[i++] = '+';
IntegerPart = (uint32)value;
DecimalPart = value - IntegerPart;
//处理整数
if( 0== IntegerPart)//整数部分为0
array[i++] = '0';
else
{
while(0 != IntegerPart)
{
array[i++ ] = IntegerPart%10 + '0';
IntegerPart/=10;
}
i --;
for(j = 1; j < (i+1)/2; j++)
{
temp = array[j];
array[j] = array[i -j+1];
array[i-j+1] = temp;
}
i++;
}
array[i++] = '.';
//处理小数
array[i++] = (uint32)(DecimalPart * 10)%10 + '0';
array[i++] = (uint32)(DecimalPart * 100)%10 + '0';
array[i++] = (uint32)(DecimalPart * 1000)%10 + '0';
array[i++] = (uint32)(DecimalPart * 10000)%10 + '0';
array[i] = '\0';
return i;
}
网上的浮点数在串口打印的函数都不太完美,于是参考他们的一些算法自己写了一个比较完善的算法
可以完成包括负数和正数的浮点数转字符串的转换
****************************************************************************************************/
#include <stdio.h>
typedef unsigned char uint8;
typedef unsigned int uint32;
uint8 Float2Char(float value,uint8 *array);
void main()
{
uint8 A[10];
float value= -123.456789;
uint8 i;
Float2Char(value,A);
printf("%s",A);
}
uint8 Float2Char(float value,uint8 *array)
{
float DecimalPart;
uint32 IntegerPart;
uint8 i = 0, j = 0, temp;
//分离整数和小数
if(value < 0)
{
value = -value ;
array[i++] = '-';
}
else
array[i++] = '+';
IntegerPart = (uint32)value;
DecimalPart = value - IntegerPart;
//处理整数
if( 0== IntegerPart)//整数部分为0
array[i++] = '0';
else
{
while(0 != IntegerPart)
{
array[i++ ] = IntegerPart%10 + '0';
IntegerPart/=10;
}
i --;
for(j = 1; j < (i+1)/2; j++)
{
temp = array[j];
array[j] = array[i -j+1];
array[i-j+1] = temp;
}
i++;
}
array[i++] = '.';
//处理小数
array[i++] = (uint32)(DecimalPart * 10)%10 + '0';
array[i++] = (uint32)(DecimalPart * 100)%10 + '0';
array[i++] = (uint32)(DecimalPart * 1000)%10 + '0';
array[i++] = (uint32)(DecimalPart * 10000)%10 + '0';
array[i] = '\0';
return i;
}
0 0
- 浮点数在串口打印的函数
- 浮点数在串口打印的函数
- 浮点数在串口打印函数
- 串口传递浮点数
- 串口接收浮点数
- 关于打印浮点数的一个问题
- putchar打印浮点数
- 浮点数转换成字符存储——串口可打印格式
- Printk不能打印浮点数!!!!
- 生成浮点数的随机函数
- LCD浮点数显示函数的探讨
- wince7下的串口信息打印函数
- 在串口中收发浮点型数据的一种方法
- 串口利用联合体收发浮点数
- 以科学计数法打印浮点数
- php浮点数四舍五入函数
- php浮点数四舍五入函数
- printf()函数和浮点数
- 哲理故事300篇(上)
- Android JSON解析
- 【读书笔记】鸟哥的Linux私房菜---第16章:例行性工作(crontab)
- MongoDB(四)——管理架构
- 第十二周 项目一 程序阅读3-2
- 浮点数在串口打印的函数
- mysql下如何执行sql脚本
- Wireshark与tcpdump介绍与使用
- ExtJS002
- 拷贝构造函数
- 关于AFNetworking的使用
- 【读书笔记】鸟哥的Linux私房菜---第19章:认识和分析日志文件
- mysql命令行启动问题
- UVA - 11995 I Can Guess the Data Structure!