반응형









출처:

http://sheepdogguides.com/arduino/aht2printfloat.htm


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*Program to provide and demonstrate a way
  to send floating point numbers to the serial
  stream.*/
 
double x;
double y;
double z;
 
void printDouble( double val, unsigned int precision){
/* prints val with number of decimal places determine by precision
   NOTE: precision is 1 followed by the number of zeros for the
   desired number of decimal places
   Example:
   printDouble( 3.1415, 100); // prints 3.14 (two decimal places)
   */
 
    Serial.print (int(val));  //prints the int part
    Serial.print("."); // print the decimal point
    unsigned int frac;
    if(val >= 0)
      frac = (val - int(val)) * precision;
    else
      frac = (int(val)- val ) * precision;
    Serial.println(frac,DEC) ;
}
 
void  setup(){
  Serial.begin(9600);
  Serial.println("Print floating point example");
  printDouble( 3.1415100);
       /* Previous line is an example of call to
          printDouble to print pi to two decimal places*/
  x = 10;
  y = 3.1;
  z = x / y;
  delay(3000);// Give reader a chance to see the output.
}
 
 
 void loop(){
   printDouble(z,10);   // one decimal place
   printDouble(z,100);  // two decimal places
   printDouble(z,1000); // three decimal places
   z = z + .1;
   delay(100);
 }
cs


반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts