home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / INTLDEMO.PAK / FMTVAL.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  1KB  |  52 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //   fmtval.cpp
  4. //----------------------------------------------------------------------------
  5. #include <owl\owlpch.h>
  6. #include <math.h>
  7. #include <locale.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. char* formatVal(char* s, long l)
  12. {
  13.   lconv * conv = localeconv();
  14.  
  15.   sprintf(s, "%ld", l);
  16.  
  17.   int group = *conv -> grouping;
  18.  
  19.   if (!group)
  20.     return s; // no grouping info, quit
  21.  
  22.   int sLen = strlen(s);
  23.  
  24.   if (sLen <= group) // not enough chars to worry about.
  25.     return s;
  26.  
  27.   int tempLen =  sLen + (sLen /3);
  28.   if (!(int)fmod(sLen, 3))
  29.     tempLen--;
  30.  
  31.   char tempStr[30];
  32.   tempStr[tempLen--] = s[sLen--];
  33.  
  34.   while (sLen >= group)
  35.   {
  36.     int groupCount = 0;
  37.     while (groupCount < group)
  38.     {
  39.       tempStr[tempLen--] = s[sLen--];
  40.       groupCount++;
  41.     }
  42.     tempStr[tempLen--] = *(conv -> thousands_sep);
  43.   } 
  44.  
  45.   while (sLen >= 0)
  46.     tempStr[tempLen--] = s[sLen--];
  47.  
  48.   strcpy(s, tempStr);
  49.  
  50.   return s;
  51. }
  52.