/npk/cli/helper_commify.hpp
http://npk.googlecode.com/ · C++ Header · 56 lines · 52 code · 4 blank · 0 comment · 20 complexity · 5986302acbb195991e47ece607d7a8be MD5 · raw file
- #pragma warning( disable : 4996 )
- #ifdef NPK_PLATFORM_LINUX
- #include <string.h>
- #endif
- void commify(double val, char *buf, int round, int comma = 3)
- {
- char temp[255];
- sprintf( temp, "%f", val );
-
- char *tc = &temp[0], *bc = buf;
- char *pc = strchr( temp, '.' );
- int underpoint = -1;
- while( *tc != '\0' )
- {
- if( ( *tc >= '0' ) && ( *tc <= '9' ) )
- {
- *bc = *tc;
- ++bc;
- if( underpoint >= 0 )
- {
- ++underpoint;
- if( round == underpoint )
- break;
- }
- }
- else if( *tc == '.' )
- {
- if( round == 0 )
- break;
- else
- {
- *bc = '.';
- ++bc;
- underpoint = 0;
- }
- }
- else if( *tc == '-' )
- {
- *bc = '.';
- ++bc;
- }
- ++tc;
- if( underpoint < 0 )
- {
- if( ( ( ( pc - tc ) % comma ) == 0 ) && ( tc != pc ) )
- {
- *bc = ',';
- ++bc;
- }
- }
- }
- *bc = '\0';
- }