forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloathelper.c
More file actions
69 lines (63 loc) · 1.22 KB
/
floathelper.c
File metadata and controls
69 lines (63 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <kdbhelper.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "floathelper.h"
char * elektraFtoA (char * buffer, ssize_t bufSize, double val)
{
if (buffer == NULL)
{
bufSize = MAX_CHARS_DOUBLE;
buffer = elektraMalloc (bufSize);
}
snprintf (buffer, bufSize, "%g", val);
struct lconv * locale;
locale = localeconv ();
char sysSep = (locale->decimal_point)[0];
if (sysSep == ELEKTRA_DEFAULT_DECIMAL_POINT)
{
return buffer;
}
else
{
char * sepPtr = strchr (buffer, sysSep);
if (sepPtr == NULL)
{
return buffer;
}
else
{
*sepPtr = ELEKTRA_DEFAULT_DECIMAL_POINT;
return buffer;
}
}
}
double elektraEFtoF (const char * string)
{
char * buffer = elektraMalloc (elektraStrLen (string));
strcpy (buffer, string);
char * sepPtr = strchr (buffer, ELEKTRA_DEFAULT_DECIMAL_POINT);
if (sepPtr == NULL)
{
elektraFree (buffer);
return atof (string);
}
else
{
struct lconv * locale;
locale = localeconv ();
char sysSep = (locale->decimal_point)[0];
*sepPtr = sysSep;
double retval = atof (buffer);
elektraFree (buffer);
return retval;
}
}