PageRenderTime 20ms CodeModel.GetById 2ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/npk/cli/helper_commify.hpp

http://npk.googlecode.com/
C++ Header | 56 lines | 52 code | 4 blank | 0 comment | 19 complexity | 5986302acbb195991e47ece607d7a8be MD5 | raw file
 1#pragma warning( disable : 4996 )
 2#ifdef NPK_PLATFORM_LINUX
 3#include <string.h>
 4#endif
 5void commify(double val, char *buf, int round, int comma = 3)
 6{
 7    char temp[255];
 8    sprintf( temp, "%f", val );
 9    
10    char *tc = &temp[0], *bc = buf;
11    char *pc = strchr( temp, '.' );
12
13    int underpoint = -1;
14    while( *tc != '\0' )
15    {
16        if( ( *tc >= '0' ) && ( *tc <= '9' ) )
17        {
18            *bc = *tc;
19            ++bc;
20
21            if( underpoint >= 0 )
22            {
23                ++underpoint;
24                if( round == underpoint )
25                    break;
26            }
27        }
28        else if( *tc == '.' )
29        {
30            if( round == 0 )
31                break;
32            else
33            {
34                *bc = '.';
35                ++bc;
36                underpoint = 0;
37            }
38        }
39        else if( *tc == '-' )
40        {
41            *bc = '.';
42            ++bc;
43        }
44
45        ++tc;
46        if( underpoint < 0 )
47        {
48            if( ( ( ( pc - tc ) % comma ) == 0 ) && ( tc != pc ) )
49            {
50                *bc = ',';
51                ++bc;
52            }
53        }
54    }
55    *bc = '\0';
56}