Gustavo Ramirez / Mbed 2 deprecated FLTtoChar

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
tony63
Date:
Sun May 03 22:52:59 2015 +0000
Commit message:
Flotante a caracter para pasar de lotante a caracter uselo con Internet de las cosas

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun May 03 22:52:59 2015 +0000
@@ -0,0 +1,78 @@
+// C program for implementation of ftoa()
+#include "mbed.h"
+#include<stdio.h>
+#include<math.h>
+Serial pc(USBTX,USBRX); 
+// reverses a string 'str' of length 'len'
+// driver program to test above funtion
+
+
+void reverse(char *str, int len)
+{
+    int i=0, j=len-1, temp;
+    while (i<j)
+    {
+        temp = str[i];
+        str[i] = str[j];
+        str[j] = temp;
+        i++; j--;
+    }
+}
+ 
+ // Converts a given integer x to string str[].  d is the number
+ // of digits required in output. If d is more than the number
+ // of digits in x, then 0s are added at the beginning.
+int intToStr(int x, char str[], int d)
+{
+    int i = 0;
+    while (x)
+    {
+        str[i++] = (x%10) + '0';
+        x = x/10;
+    }
+ 
+    // If number of digits required is more, then
+    // add 0s at the beginning
+    while (i < d)
+        str[i++] = '0';
+ 
+    reverse(str, i);
+    str[i] = '\0';
+    return i;
+}
+ 
+// Converts a floating point number to string.
+void ftoa(float n, char *res, int afterpoint)
+{
+    // Extract integer part
+    int ipart = (int)n;
+ 
+    // Extract floating part
+    float fpart = n - (float)ipart;
+ 
+    // convert integer part to string
+    int i = intToStr(ipart, res, 0);
+ 
+    // check for display option after point
+    if (afterpoint != 0)
+    {
+        res[i] = '.';  // add dot
+ 
+        // Get the value of fraction part upto given no.
+        // of points after dot. The third parameter is needed
+        // to handle cases like 233.007
+        float fp=10;
+        fpart =fpart * pow(fp,afterpoint);
+        
+        intToStr((int)fpart, res + i + 1, afterpoint);
+    }
+}
+
+int main()
+{
+    char res[20];
+    float n = 233.007;
+    ftoa(n, res, 4);
+    pc.printf("\n\"%s\"\n", res);
+    return 0;
+} 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun May 03 22:52:59 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/8ab26030e058
\ No newline at end of file