#include <stdlib.h>#include <stdio.h>#include <ctype.h>#include <math.h>Go to the source code of this file.
Defines | |
| #define | EPSILON ((1.0/256.0)/2.0) |
| #define | GET_DEC(x) ( (x) - (float)(int)(x) ) |
Functions | |
| char * | price_to_str (float price, int *numorator, int *demoninator, int *decimal, char *fltfmt) |
| #define EPSILON ((1.0/256.0)/2.0) |
| #define GET_DEC | ( | x | ) | ( (x) - (float)(int)(x) ) |
| char* price_to_str | ( | float | price, | |
| int * | numorator, | |||
| int * | demoninator, | |||
| int * | decimal, | |||
| char * | fltfmt | |||
| ) |
Definition at line 24 of file price_conv.cpp.
References EPSILON, and GET_DEC.
Referenced by out_graph().
00029 { 00030 static char rtn[64]; 00031 int whole = (int)price; 00032 float dec = GET_DEC( price ), 00033 numr; 00034 /* float pow( double, double ); */ 00035 00036 // caller doesn't want fractions 00037 if( fltfmt ) 00038 { 00039 sprintf( rtn, fltfmt, price ); 00040 *numorator = *demoninator = *decimal = 0; 00041 return rtn; 00042 } 00043 00044 numr = dec * 256; 00045 /* check if we have a perfect fration in 256ths */ 00046 { 00047 float rdec = GET_DEC( numr ); 00048 00049 if( rdec < EPSILON ) 00050 ; /* close enough to frac */ 00051 else if( (1-rdec) < EPSILON ) /* just over but close enough */ 00052 ++numr; 00053 else /* no frac match */ 00054 { 00055 sprintf( rtn, "%f", price ); 00056 *numorator = *demoninator = *decimal = 0; 00057 return rtn; 00058 } 00059 } 00060 00061 /* now have numr 256ths */ 00062 /* resolve down */ 00063 if( numr != 0 ) 00064 { 00065 int cnt = 8; 00066 00067 while( (float)(numr)/2.0 == (float)(int)(numr/2) ) 00068 { 00069 numr /= 2; 00070 --cnt; 00071 } 00072 00073 /* don't want both whole AND numerator to be - */ 00074 if( whole<0 && numr<0.0 ) 00075 numr = -numr; 00076 *numorator = (int)numr; 00077 *demoninator = (int)pow(2.0, (float)cnt); 00078 *decimal = whole; 00079 sprintf( rtn, "%d %d/%d", whole, 00080 (int)numr, 00081 *demoninator ); 00082 } 00083 else 00084 { 00085 *numorator = *demoninator = 0; 00086 *decimal = whole; 00087 sprintf( rtn, "%d", whole ); 00088 } 00089 00090 return rtn; 00091 }
1.5.1