GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-b4e4cb0fe9
commas.c
Go to the documentation of this file.
1 /*!
2  * \file lib/gis/commas.c
3  *
4  * \brief GIS Library - Comma string functions.
5  *
6  * (C) 2001-2014 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public License
9  * (>=v2). Read the file COPYING that comes with GRASS for details.
10  *
11  * \author GRASS GIS Development Team
12  *
13  * \date 1999-2014
14  */
15 
16 #include <string.h>
17 #include <grass/gis.h>
18 
19 /**
20  * \brief Inserts commas into a number string.
21  *
22  * Examples:
23  *
24  * - 1234567 becomes 1,234,567
25  * - 1234567.89 becomes 1,234,567.89
26  * - 12345 becomes 12,345
27  * - 1234 stays 1234
28  *
29  * <b>Note:</b> Does not work with negative numbers.
30  *
31  * \param[in,out] buf string
32  * \return 1 if no commas inserted
33  * \return 0 if commas inserted
34  */
35 
36 int G_insert_commas(char *buf)
37 {
38  char number[100];
39  int i, len;
40  int comma;
41 
42  while (*buf == ' ')
43  buf++;
44  strcpy(number, buf);
45  for (len = 0; number[len]; len++)
46  if (number[len] == '.')
47  break;
48  if (len < 5)
49  return 1;
50 
51  i = 0;
52  if ((comma = len % 3)) {
53  while (i < comma)
54  *buf++ = number[i++];
55  *buf++ = ',';
56  }
57 
58  for (comma = 0; number[i]; comma++) {
59  if (number[i] == '.')
60  break;
61  if (comma && (comma % 3 == 0))
62  *buf++ = ',';
63  *buf++ = number[i++];
64  }
65  while (number[i])
66  *buf++ = number[i++];
67  *buf = 0;
68 
69  return 0;
70 }
71 
72 /**
73  * \brief Removes commas from number string.
74  *
75  * Examples:
76  * - 1,234,567 becomes 1234567<br>
77  * - 1,234,567.89 becomes 1234567.89<br>
78  * - 12,345 becomes 12345<br>
79  * - 1234 stays 1234
80  *
81  * \param[in,out] buf string
82  * \return
83  */
84 
85 void G_remove_commas(char *buf)
86 {
87  char *b;
88 
89  for (b = buf; *b; b++)
90  if (*b != ',')
91  *buf++ = *b;
92 
93  *buf = 0;
94 }
void G_remove_commas(char *buf)
Removes commas from number string.
Definition: commas.c:85
int G_insert_commas(char *buf)
Inserts commas into a number string.
Definition: commas.c:36
#define strcpy
Definition: parson.c:62
double b
Definition: r_raster.c:39