commas.c

Go to the documentation of this file.
00001 #include "gis.h"
00002 #include <string.h>
00003 /* puts commas into numbers:
00004         1234567    becomes 1,234,567
00005         1234567.89 becomes 1,234,567.89
00006         12345      becomes 12,345
00007         1234       stays   1234
00008 * doesn't work well with negative numbers (yet)
00009 */
00010 int G_insert_commas(
00011     char *buf)
00012 {
00013     char number[100];
00014     int i,len;
00015     int comma;
00016 
00017     while (*buf == ' ') buf++;
00018     strcpy (number, buf);
00019     for (len=0; number[len]; len++)
00020         if(number[len] == '.')
00021             break;
00022     if (len < 5)
00023         return 1;
00024  
00025     i = 0;
00026     if (comma = len%3)
00027     {
00028         while (i < comma)
00029             *buf++ = number[i++];
00030         *buf++ = ',';
00031     }
00032     for (comma = 0; number[i]; comma++)
00033     {
00034         if (number[i] == '.')
00035             break;
00036         if (comma && (comma%3 == 0))
00037             *buf++ = ',';
00038         *buf++ = number[i++];
00039     }
00040     while (number[i])
00041         *buf++ = number[i++];
00042     *buf = 0;
00043 
00044     return 0;
00045 }
00046 
00047 /* removes commas from strings representing a number 
00048         1,234,567    becomes 1234567
00049         1,234,567.89 becomes 1234567.89
00050         12,345      becomes 12345
00051         1234       stays   1234
00052 */
00053 #include <string.h>
00054 
00055 int G_remove_commas(
00056     char *buf)
00057 {
00058     char *b;
00059 
00060     for (b=buf; *b; b++)
00061         if(*b != ',')
00062             *buf++ = *b;
00063 
00064     *buf = 0;
00065     return  0;
00066 }

Generated on Sat Jul 22 22:06:14 2006 for GRASS by  doxygen 1.4.7