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

◆ format_float()

fl::string fl::printf_detail::format_float ( float value,
int precision )
inline

Definition at line 282 of file stdio.h.

282 {
283 if (precision < 0) {
284 // Default precision - use sstream's default behavior
285 sstream stream;
286 stream << value;
287 return stream.str();
288 }
289
290 // Simple precision formatting
291 // This is a basic implementation - could be enhanced
292 if (precision == 0) {
293 int int_part = static_cast<int>(value + 0.5f); // Round
294 sstream stream;
295 stream << int_part;
296 return stream.str();
297 }
298
299 // For non-zero precision, use basic rounding
300 int multiplier = 1;
301 for (int i = 0; i < precision; ++i) {
302 multiplier *= 10;
303 }
304
305 // Handle rounding correctly for both positive and negative numbers
306 float scaled_float = value * multiplier;
307 int scaled;
308 if (scaled_float >= 0) {
309 scaled = static_cast<int>(scaled_float + 0.5f);
310 } else {
311 scaled = static_cast<int>(scaled_float - 0.5f);
312 }
313
314 int int_part = scaled / multiplier;
315 int frac_part = scaled % multiplier;
316
317 // For negative numbers, frac_part will be negative, so take absolute value
318 if (frac_part < 0) {
319 frac_part = -frac_part;
320 }
321
322 sstream stream;
323 // Preserve sign for values like -0.5 that round to int_part=0 (printed
324 // without sign by the integer write below).
325 if (value < 0 && int_part == 0) {
326 stream << "-";
327 }
328 stream << int_part;
329 stream << ".";
330
331 // Emit exactly `precision` fractional digits, zero-padded on the left.
332 // The previous implementation stopped padding once temp_multiplier dropped
333 // to 1, which produced "0.0"/"1.0" instead of "0.00"/"1.00" for any
334 // integer-valued input. It also omitted the digits entirely when
335 // frac_part == 0.
336 int temp_multiplier = multiplier / 10;
337 while (temp_multiplier > 0) {
338 int digit = (frac_part / temp_multiplier) % 10;
339 stream << static_cast<char>('0' + digit);
340 temp_multiplier /= 10;
341 }
342
343 return stream.str();
344}
string str() const FL_NOEXCEPT
Definition strstream.h:43
constexpr int type_rank< T >::value

References FL_NOEXCEPT, fl::sstream::str(), and fl::type_rank< T >::value.

Referenced by format_arg(), and fl::ftoa().

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