Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

//**************************************

//     

//INCLUDE files for :Format doubles usin

//     g commas and variable precision

//**************************************

//     

/* +++Date last modified: 05-Jul-1997 */

/*

** NUMCNVRT.H - Header file for SNIPPETS numerical <=> string conversions

*/

#ifndef NUMCNVRT__H

#define NUMCNVRT__H

#include  /* for size_t */

#include "sniptype.h"

#include "round.h"

#include "pi.h"

#define R_ERROR -2 /* EVAL.C Range error*/

/*

** Callable library functions begin here

*/

char * base_convert(const char *in, char *out,

size_t len, int rin, int rout);/* Bascnvrt.C */

char * comma_float(double num, char *buf, int dec); /* Commaflt.C */

size_t commafmt(char *buf, int bufsize, long N); /* Commafmt.C */

char * eng(double value, int places);/* Eng.C */

int evaluate(char *line, double *val);/* Eval.C */

char * fmt_money(double amt);/* Fmtmoney.C */

longhexorint(const char *string); /* Hexorint.C */

char * ltostr(long num, char *string,

size_t max_chars, unsigned base); /* Ltostr.C*/

char * ordinal_text(int number); /* Ord_Text.C */

int scanfrac (const char buf[], double *f);/* Scanfrac.C */

unsigned int hstr_i(char *cptr); /* Hstr_I.C*/

char *long2roman(long val, char *buf, size_t buflen); /* L2Roman.C */

long roman2long(const char *str);/* Roman2L.C */

#if defined(__ZTC__) && !defined(__SC__)

char * ltoa(long val, char *buf, int base); /* Ltoa.C */

#endif

/*

** File: STR27SEG.C

*/

    struct Seg7disp {

    unsigned seg_a : 1;

    unsigned seg_b : 1;

    unsigned seg_c : 1;

    unsigned seg_d : 1;

    unsigned seg_e : 1;

    unsigned seg_f : 1;

    unsigned seg_g : 1;

};

char *str27seg(char *string);

#endif /* NUMCNVRT__H */

 

code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

//     

/* +++Date last modified: 05-Jul-1997 */

/*

** COMMAFLT.C - Format a double using commas as thousands separators

**and a specified number of significant fractional digits.

**

** public domain by Bruce Wedding and Kurt Kuzba

*/

#include 

#include "numcnvrt.h"

#include "snip_str.h"

char *comma_float( double num, char *buf, int dec)

    {

    char tmp[80];

    int bf = 0, cm = 0, tm = 9 - dec + (!dec);

    sprintf(tmp, "%.9f", num);

    strrev(tmp);

    if(dec)

        {

        while( (buf[bf++] = tmp[tm++]) != '.')

        ;

        while((buf[bf++] = tmp[tm++]) != 0)

            {

            if(++cm % 3 == 0 && tmp[tm])

            buf[bf++] = ',';

        }

    }

    return strrev(buf);

}

#ifdef TEST

#include 

int main( void )

    {

    int i;

    char result[80];

        double num[10] = {

        1.98765,12.98765,123.98765,

        1234.98765,12345.98765,123456.98765,

        1234567.98765, 12345678.98765, 123456789.98765,

        1234567890.98765 };

        for ( i = 0; i < 10; i++ )

        printf("Before: %-24.5f After: %s \n",

        num[i], comma_float(num[i], result, 6));

        return 0;

    }

    #endif /* TEST */