FastLED 3.9.15
Loading...
Searching...
No Matches

◆ ftoa()

static void fl::string_functions::ftoa ( float value,
char * buffer,
int precision = 2 )
static

Definition at line 29 of file str.cpp.

29 {
30
31#ifdef FASTLED_TESTING
32 // use snprintf during testing with precision
33 snprintf(buffer, 64, "%.*f", precision, value);
34 return;
35
36#else
37 // Handle negative values
38 if (value < 0) {
39 *buffer++ = '-';
40 value = -value;
41 }
42
43 // Extract integer part
44 u32 intPart = (u32)value;
45
46 // Convert integer part to string (reversed)
47 char intBuf[12]; // Enough for 32-bit integers
48 int i = 0;
49 do {
50 intBuf[i++] = '0' + (intPart % 10);
51 intPart /= 10;
52 } while (intPart);
53
54 // Write integer part in correct order
55 while (i--) {
56 *buffer++ = intBuf[i];
57 }
58
59 *buffer++ = '.'; // Decimal point
60
61 // Extract fractional part
62 float fracPart = value - (u32)value;
63 for (int j = 0; j < precision; ++j) {
64 fracPart *= 10.0f;
65 int digit = (int)fracPart;
66 *buffer++ = '0' + digit;
67 fracPart -= digit;
68 }
69
70 *buffer = '\0'; // Null-terminate
71#endif
72}
int snprintf(char *buffer, fl::size size, const char *format, const Args &... args)
Snprintf-like formatting function that writes to a buffer.
Definition printf.h:422

References ftoa(), and fl::snprintf().

Referenced by ftoa().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: