Flotante a caracter para pasar de fotante a caracter uselo con Internet de las cosas

Dependencies:   mbed

Committer:
tony63
Date:
Sun May 03 22:52:59 2015 +0000
Revision:
0:64c04c367702
Flotante a caracter para pasar de lotante a caracter uselo con Internet de las cosas

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tony63 0:64c04c367702 1 // C program for implementation of ftoa()
tony63 0:64c04c367702 2 #include "mbed.h"
tony63 0:64c04c367702 3 #include<stdio.h>
tony63 0:64c04c367702 4 #include<math.h>
tony63 0:64c04c367702 5 Serial pc(USBTX,USBRX);
tony63 0:64c04c367702 6 // reverses a string 'str' of length 'len'
tony63 0:64c04c367702 7 // driver program to test above funtion
tony63 0:64c04c367702 8
tony63 0:64c04c367702 9
tony63 0:64c04c367702 10 void reverse(char *str, int len)
tony63 0:64c04c367702 11 {
tony63 0:64c04c367702 12 int i=0, j=len-1, temp;
tony63 0:64c04c367702 13 while (i<j)
tony63 0:64c04c367702 14 {
tony63 0:64c04c367702 15 temp = str[i];
tony63 0:64c04c367702 16 str[i] = str[j];
tony63 0:64c04c367702 17 str[j] = temp;
tony63 0:64c04c367702 18 i++; j--;
tony63 0:64c04c367702 19 }
tony63 0:64c04c367702 20 }
tony63 0:64c04c367702 21
tony63 0:64c04c367702 22 // Converts a given integer x to string str[]. d is the number
tony63 0:64c04c367702 23 // of digits required in output. If d is more than the number
tony63 0:64c04c367702 24 // of digits in x, then 0s are added at the beginning.
tony63 0:64c04c367702 25 int intToStr(int x, char str[], int d)
tony63 0:64c04c367702 26 {
tony63 0:64c04c367702 27 int i = 0;
tony63 0:64c04c367702 28 while (x)
tony63 0:64c04c367702 29 {
tony63 0:64c04c367702 30 str[i++] = (x%10) + '0';
tony63 0:64c04c367702 31 x = x/10;
tony63 0:64c04c367702 32 }
tony63 0:64c04c367702 33
tony63 0:64c04c367702 34 // If number of digits required is more, then
tony63 0:64c04c367702 35 // add 0s at the beginning
tony63 0:64c04c367702 36 while (i < d)
tony63 0:64c04c367702 37 str[i++] = '0';
tony63 0:64c04c367702 38
tony63 0:64c04c367702 39 reverse(str, i);
tony63 0:64c04c367702 40 str[i] = '\0';
tony63 0:64c04c367702 41 return i;
tony63 0:64c04c367702 42 }
tony63 0:64c04c367702 43
tony63 0:64c04c367702 44 // Converts a floating point number to string.
tony63 0:64c04c367702 45 void ftoa(float n, char *res, int afterpoint)
tony63 0:64c04c367702 46 {
tony63 0:64c04c367702 47 // Extract integer part
tony63 0:64c04c367702 48 int ipart = (int)n;
tony63 0:64c04c367702 49
tony63 0:64c04c367702 50 // Extract floating part
tony63 0:64c04c367702 51 float fpart = n - (float)ipart;
tony63 0:64c04c367702 52
tony63 0:64c04c367702 53 // convert integer part to string
tony63 0:64c04c367702 54 int i = intToStr(ipart, res, 0);
tony63 0:64c04c367702 55
tony63 0:64c04c367702 56 // check for display option after point
tony63 0:64c04c367702 57 if (afterpoint != 0)
tony63 0:64c04c367702 58 {
tony63 0:64c04c367702 59 res[i] = '.'; // add dot
tony63 0:64c04c367702 60
tony63 0:64c04c367702 61 // Get the value of fraction part upto given no.
tony63 0:64c04c367702 62 // of points after dot. The third parameter is needed
tony63 0:64c04c367702 63 // to handle cases like 233.007
tony63 0:64c04c367702 64 float fp=10;
tony63 0:64c04c367702 65 fpart =fpart * pow(fp,afterpoint);
tony63 0:64c04c367702 66
tony63 0:64c04c367702 67 intToStr((int)fpart, res + i + 1, afterpoint);
tony63 0:64c04c367702 68 }
tony63 0:64c04c367702 69 }
tony63 0:64c04c367702 70
tony63 0:64c04c367702 71 int main()
tony63 0:64c04c367702 72 {
tony63 0:64c04c367702 73 char res[20];
tony63 0:64c04c367702 74 float n = 233.007;
tony63 0:64c04c367702 75 ftoa(n, res, 4);
tony63 0:64c04c367702 76 pc.printf("\n\"%s\"\n", res);
tony63 0:64c04c367702 77 return 0;
tony63 0:64c04c367702 78 }