PropWare
C++ objects and CMake build system for Parallax Propeller
Home
Download
Getting Started
Documentation
Reference
About
Related Links
Namespace List
Class List
File List
Examples
Build System
File List
File Members
propsideworkspace
Learn
Simple Libraries
TextDevices
libsimpletext
stringToFloat.c
1
/*
2
* @file floatToString.c
3
*
4
* Copyright (c) 2013 Parallax Inc.
5
* Author Andy Lindsay
6
* Optimized for size using pointers -vs- array by Steve Denson
7
*
8
* See end of file for terms of use.
9
*/
10
#include "simpletext.h"
11
12
static
int
ch2int(
char
ch)
13
{
14
return
ch -
'0'
;
15
}
16
17
static
inline
int
floatChar(
char
ch)
18
{
19
return
(ch ==
'-'
|| ch ==
'+'
|| ch ==
'.'
|| (ch >=
'0'
&& ch <=
'9'
));
20
}
21
22
float
string2float(
char
*s,
char
**end)
23
{
24
float
n = 0.0;
25
float
x = 1.0;
26
float
sign = 1.0;
27
28
char
*dp;
/* decimal point position*/
29
30
/* skip any leading non-float chars */
31
while
(!floatChar(*s))
32
s++;
33
34
if
(*s ==
'-'
) {
35
sign = -1.0;
36
s++;
37
}
38
if
(*s ==
'+'
)
39
s++;
40
41
/* get all integer digits */
42
n = 0;
43
while
((*s >=
'0'
) && (*s <=
'9'
)) {
44
n = 10.0 * n + ch2int(*s);
45
s++;
46
}
47
48
dp = s;
49
50
if
(*dp !=
'.'
) {
51
*end = dp;
/* needed for scanf */
52
return
n * sign;
53
}
54
55
x = 0.1;
56
s = dp+1;
57
/* convert decimal part */
58
for
( ; (*s >=
'0'
) && (*s <=
'9'
); s++) {
59
n += (x * ch2int(*s));
60
x /= 10.0;
61
}
62
*end = s;
/* need for scanf */
63
return
n * sign;
64
}
65
66
67
/*
68
+--------------------------------------------------------------------
69
| TERMS OF USE: MIT License
70
+--------------------------------------------------------------------
71
Permission is hereby granted, free of charge, to any person obtaining
72
a copy of this software and associated documentation files
73
(the "Software"), to deal in the Software without restriction,
74
including without limitation the rights to use, copy, modify, merge,
75
publish, distribute, sublicense, and/or sell copies of the Software,
76
and to permit persons to whom the Software is furnished to do so,
77
subject to the following conditions:
78
79
The above copyright notice and this permission notice shall be
80
included in all copies or substantial portions of the Software.
81
82
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
83
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
84
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
85
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
86
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
87
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
88
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
89
+--------------------------------------------------------------------
90
*/
Generated on Sat Aug 27 2016 00:58:09 for PropWare by
1.8.12