如何获得一个十进制数为Arduino的或C两个整数的整数和小数部分? [英] How to get the integer and fractional part of a decimal number as two integers in Arduino or C?

查看:168
本文介绍了如何获得一个十进制数为Arduino的或C两个整数的整数和小数部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何转换成30.8365146两个整数,30和8365146,例如,在Arduino的或C?

此问题面临我,当我尝试通过的 GPS 数据=HTTP ://en.wikipedia.org/wiki/XBee相对=nofollow>的XBee系列 1不容许传输率的数字,所以我决定将数据分割成两部分。我怎样才能做到这一点?

我已经试过这样的事情:

 双击NUM = 30.233;
诠释A,B;A =地板(NUM);
B =(NUM-A)* POW(10,3);

输出为 30 232 !输出不是30和233为什么,我该如何解决这个问题?


解决方案

 双值= 30.8365146;
INT left_part,right_part;
字符缓冲区[50];
sprintf的(缓冲,%LF,值);
sscanf的(缓冲,%D,&安培; left_part,&安培; right_part);

和你会得到左/单独存储在正确的整数部分。

P.S。另一个解决方案是由10一些力量,只是乘以你的电话号码,并发送为整数。

How do I convert 30.8365146 into two integers, 30 and 8365146, for example, in Arduino or C?

This problem faces me when I try to send GPS data via XBee series 1 which don't allow to transmit fraction numbers, so I decided to split the data into two parts. How can I do this?

I have tried something like this:

double num=30.233;
int a,b;

a = floor(num); 
b = (num-a) * pow(10,3);

The output is 30 and 232! The output is not 30 and 233. Why and how can I fix it?

解决方案

double value = 30.8365146;
int left_part, right_part;
char buffer[50];
sprintf(buffer, "%lf", value);
sscanf(buffer, "%d.%d", &left_part, &right_part);

and you will get left/right parts separately stored in integers.

P.S. the other solution is to just multiply your number by some power of 10 and send as an integer.

这篇关于如何获得一个十进制数为Arduino的或C两个整数的整数和小数部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆