You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// remove the prepended zeros and (if possible) the decimal point in a float that's formated as string. e.g. "47.1100815000000" => "47.1100815" or "8.0000E-12" => "8E-12"
15
+
voidnf_RemovePrependedZeros(char* floatStr)
16
+
{
17
+
int length = hal_strlen_s(floatStr);
18
+
// flag for finding the decimal point
19
+
bool pointFound = false;
20
+
// if not -1 we found the first zero after the decimal point
21
+
int firstZero = -1;
22
+
// if not -1 we found an "e" or "E" after the last zero of this is the string length
23
+
int nextNonZero = length;
24
+
25
+
// iterate thru all chars
26
+
for (int i = 0; i < length; i++)
27
+
{
28
+
// no decimal point found until now?
29
+
if (!pointFound)
30
+
{
31
+
// is it the decimal point?
32
+
if (floatStr[i] == '.')
33
+
{
34
+
pointFound = true;
35
+
}
36
+
37
+
// next char
38
+
continue;
39
+
}
40
+
41
+
// at this point we found the decimal point
42
+
// no zero found until now?
43
+
if (firstZero == -1)
44
+
{
45
+
// is it a zero?
46
+
if (floatStr[i] == '0')
47
+
{
48
+
// store the position of the first zero after the decimal point
49
+
firstZero = i;
50
+
}
51
+
52
+
// next char
53
+
continue;
54
+
}
55
+
56
+
// at this point we found the decimal point and the first zero
57
+
// an "e" or "E" char stops the sequence of zeros
58
+
if (floatStr[i] == 'e' || floatStr[i] == 'E')
59
+
{
60
+
// store the position of the e/E char
61
+
nextNonZero = i;
62
+
// done! we found the positions for the prepended zeros
63
+
break;
64
+
}
65
+
66
+
// at this point we found the decimal point and the first zero and the current char is not the e/E char
67
+
// is this not a zero?
68
+
if (floatStr[i] != '0')
69
+
{
70
+
// reset! we need to find another zero
71
+
firstZero = -1;
72
+
}
73
+
}
74
+
75
+
// something to remove?
76
+
if (pointFound && firstZero != -1 && nextNonZero != -1)
77
+
{
78
+
// is the char before the first zero the decimal point? => Remove the decimal point
0 commit comments