libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
writer_float_fixed.hpp
1#pragma once
2
19template <typename Float>
20bool Writer::FormatFixedText(Float value, uint8_t precision, bool alternate, char* out,
21 size_t& out_size)
22{
23 Float rounded = RoundDecimal(value, precision);
24 if (!std::isfinite(rounded))
25 {
26 return false;
27 }
28 auto normalized = NormalizeDecimal(rounded);
29 int integer_exponent = (rounded == 0) ? 0 : normalized.exponent;
30 int start_pos = (integer_exponent > 0) ? integer_exponent : 0;
31 Float scale = Power10<Float>(start_pos);
32
33 out_size = 0;
34 for (int pos = start_pos; pos >= 0; --pos)
35 {
36 if (!AppendBufferChar(out, float_buffer_capacity, out_size,
37 static_cast<char>('0' + ExtractDigit(rounded, scale))))
38 {
39 return false;
40 }
41 scale /= 10;
42 }
43
44 if (precision != 0 || alternate)
45 {
46 if (!AppendBufferChar(out, float_buffer_capacity, out_size, '.'))
47 {
48 return false;
49 }
50 }
51
52 for (uint8_t i = 0; i < precision; ++i)
53 {
54 if (!AppendBufferChar(out, float_buffer_capacity, out_size,
55 static_cast<char>('0' + ExtractDigit(rounded, scale))))
56 {
57 return false;
58 }
59 scale /= 10;
60 }
61
62 return true;
63}