/*
///////////////////////////////////////////////Credits/////////////////////////////////////////////
// File Name: Function to convert float to cstring
// Copyright: Travis Sidelinger (2006Jun14)
//
// License:
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
*/
 
/*>-----------------------------------------< Declares >----------------------------------------<*/
 
#define BUF 8
 
/*>---------------------------------------< Header_Files >--------------------------------------<*/
 
#include <stdio.h>
 
/*>----------------------------------------< Functions >----------------------------------------<*/
 
/* Float to char */
char *f2c(float fltnum, unsigned char numdigits)
{
    /* Variables */
    static char chrflt[BUF]      ;            /* Returned char string */
    auto unsigned char pos    = 0;            /* Current position in string */
    auto unsigned char i      = 0;            /* Used for loop tracking */
    auto unsigned int x       = 0;            /* Temp integer used for various items */
    auto unsigned char flagDecimal = 0;       /* Used to track when to incert the decimal point */
    auto unsigned int intsize = 1;            /* Used to count the size of the integer portion of numbers */
 
    /* Main */
 
        /* Make sure we do not over flow the buffer */
        if(numdigits > BUF-1) { return(chrflt); }
 
    /* Determine sign */
    if((int)(fltnum) < 1)
    {
        if((int)(fltnum) < 0) { chrflt[pos] = '-'; pos++; }
        else                  { chrflt[pos] = '0'; pos++; }
        }
 
    /* Determine each digit */
    for(i=numdigits;i>0;i--)
    {
        if((int)(fltnum) < 1)
        {
            fltnum *= 10;
            x = ((unsigned int)(fltnum)) % 10;                                  /* get the most significant digit */
            fltnum = fltnum - (float)(x);                                       /* remove the most significant digit */
 
            /* Add the decimal point when passing 1 */
            if(flagDecimal == 0)
            {
                chrflt[pos] = '.';
                pos++;
                flagDecimal = 1;
                }
            }
        else
        {
            intsize = 1;
            while(((int)(fltnum) / intsize) > 10) { intsize = intsize * 10; }   /* count the 10's of the integer portion */
            x = ((unsigned int)(fltnum)) / intsize ;                            /* get the most significant digit */
            fltnum = (fltnum - (float)(x*intsize));                             /* remove the most significant digit */
            }
        chrflt[pos] = x + 48;                                                   /* Convert to ASCII */
        pos++;
        }
 
    chrflt[pos] = '\0';                                                         /* Add null termination */
 
    /* End */
    return(chrflt);
    }
 
/*>-------------------------------------------< END >-------------------------------------------<*/
/var/www/sites/dokuwiki-2011-05-25a/data/pages/projects/devry/f2c.c.txt · Last modified: 2009/04/11 22:23 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki