перевод из float в іnt
- Войдите на сайт для отправки комментариев
Ср, 26/10/2016 - 18:18
В общем хочу зделать так штоби float к римеру 21.60 превести его в int 216.
Нужно ето для создания петеостанции с отображениям градусов и влажности в десятках.
Вот код (P.S. не хватает именно функии описаной више).
#include "DHT.h"
#include "LedControlMS.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
int ht = 0;
int tt = 0;
int hh = 0;
int th = 0;
char mm = ' ';
int cof = 0;
LedControl lc=LedControl(12,11,10,1);
unsigned long delaytime=250;
void setup()
{
lc.shutdown(0,false);
lc.setIntensity(0,8);
lc.clearDisplay(0);
Serial.begin(57600);
pinMode(2, INPUT);
digitalWrite(2, HIGH);
pinMode(3, INPUT);
digitalWrite(3, HIGH);
dht.begin();
}
void writeOn7Segment() {
lc.setChar(0,0,'C',false);
lc.setChar(0,1,ht,false);
lc.setChar(0,2,tt,false);
lc.setChar(0,3,mm,false);
lc.setChar(0,4,'H',false);
lc.setChar(0,5,hh,false);
lc.setChar(0,6,th,false);
lc.setChar(0,7,' ',false);
}
void loop() {
writeOn7Segment();
float h = dht.readHumidity();
float t = dht.readTemperature();
int i = 0;
if(t<0){
mm = '-';
t = t*-1;
cof = 1;
}else{
mm = ' ' ;
cof = 0;
}
if(h>90){
hh = hh + 1;
}
ht =(int) t;
th =(int) h;
ht = t % 10;
tt = (t / 10) % 10;
hh = h % 10;
th = (h / 10) % 10;
Serial.print("loading");
do
{
Serial.print(".");
delay(100);
i++;
} while (i<9);
i = 0;
Serial.println(".");
delay(100);
Serial.print("Hum: ");
Serial.print(h);
Serial.print(" %");
Serial.print(" Temp: ");
if (cof>0){
Serial.print(t*-1);
}else{
Serial.print(t);
}
Serial.println(" C ");
}
В общем хочу зделать так штоби float к римеру 21.60 превести его в int 216.
Достаточно просто умножить на 10 и поместить в int.
int x = (float)y * 10;
не работает -_-
#include "DHT.h" #include "LedControlMS.h" #define DHTPIN 2 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); int ht = 0; int tt = 0; int ht2 = 0; int th2 = 0; int hh = 0; int th = 0; int ttwo = 0; int htwo = 0; char mm = ' '; int cof = 0; LedControl lc=LedControl(12,11,10,1); unsigned long delaytime=250; void setup() { lc.shutdown(0,false); lc.setIntensity(0,8); lc.clearDisplay(0); Serial.begin(57600); pinMode(2, INPUT); digitalWrite(2, HIGH); pinMode(3, INPUT); digitalWrite(3, HIGH); dht.begin(); } void writeOn7Segment() { lc.setChar(0,0,ht2,false); lc.setChar(0,1,tt,true); lc.setChar(0,2,ht,false); lc.setChar(0,3,mm,false); lc.setChar(0,4,th2,false); lc.setChar(0,5,th,true); lc.setChar(0,6,hh,false); lc.setChar(0,7,' ',false); } void loop() { writeOn7Segment(); float h = dht.readHumidity(); float t = dht.readTemperature(); float hic = dht.computeHeatIndex(t, h, false); int i = 0; if(t<0){ mm = '-'; t = t*-1; cof = 1; }else{ mm = ' ' ; cof = 0; } if(h>90){ hh = hh + 1; } ttwo =(float)t*10; htwo =(float)h*10; ht = ttwo % 10; tt = (ttwo / 10) % 10; ht2 = (ttwo / 100) % 10; hh = htwo % 10; th = (htwo / 10) % 10; th2 = (htwo / 100) % 10; Serial.print("loading"); do { Serial.print("."); delay(100); i++; } while (i<9); i = 0; Serial.println("."); delay(100); Serial.print("Hum: "); Serial.print(h); Serial.print(" %"); Serial.print(" Temp: "); if (cof>0){ Serial.print(t*-1); }else{ Serial.print(t); } Serial.println(" C "); }Вот простой пример разделения целой и дробной частей float-числа :