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