GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 2001-2014 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author GRASS Development Team
10 *
11 * \date 1999-2014
12 */
13
14#include <string.h>
15#include <grass/gis.h>
16
17/**
18 * \brief Inserts commas into a number string.
19 *
20 * Examples:
21 *
22 * - 1234567 becomes 1,234,567
23 * - 1234567.89 becomes 1,234,567.89
24 * - 12345 becomes 12,345
25 * - 1234 stays 1234
26 *
27 * <b>Note:</b> Does not work with negative numbers.
28 *
29 * \param[in,out] buf string
30 * \return 1 if no commas inserted
31 * \return 0 if commas inserted
32 */
33int G_insert_commas(char *buf)
34{
35 char number[100];
36 int i, len;
37 int comma;
38
39 while (*buf == ' ')
40 buf++;
41 G_strlcpy(number, buf, sizeof(number));
42 for (len = 0; number[len]; len++)
43 if (number[len] == '.')
44 break;
45 if (len < 5)
46 return 1;
47
48 i = 0;
49 if ((comma = len % 3)) {
50 while (i < comma)
51 *buf++ = number[i++];
52 *buf++ = ',';
53 }
54
55 for (comma = 0; number[i]; comma++) {
56 if (number[i] == '.')
57 break;
58 if (comma && (comma % 3 == 0))
59 *buf++ = ',';
60 *buf++ = number[i++];
61 }
62 while (number[i])
63 *buf++ = number[i++];
64 *buf = 0;
65
66 return 0;
67}
68
69/**
70 * \brief Removes commas from number string.
71 *
72 * Examples:
73 * - 1,234,567 becomes 1234567<br>
74 * - 1,234,567.89 becomes 1234567.89<br>
75 * - 12,345 becomes 12345<br>
76 * - 1234 stays 1234
77 *
78 * \param[in,out] buf string
79 * \return
80 */
81void G_remove_commas(char *buf)
82{
83 char *b;
84
85 for (b = buf; *b; b++)
86 if (*b != ',')
87 *buf++ = *b;
88
89 *buf = 0;
90}
void G_remove_commas(char *buf)
Removes commas from number string.
Definition commas.c:81
int G_insert_commas(char *buf)
Inserts commas into a number string.
Definition commas.c:33
size_t G_strlcpy(char *, const char *, size_t)
Safe string copy function.
Definition strlcpy.c:54
double b
Definition r_raster.c:37