-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
MQTT: fix float format #27046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MQTT: fix float format #27046
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,17 @@ func TestMqttNaNInf(t *testing.T) { | |||||
| assert.Equal(t, "+Inf", m.encode(math.Inf(0)), "Inf not encoded as string") | ||||||
| } | ||||||
|
|
||||||
| func TestMqttEncodeFloat(t *testing.T) { | ||||||
| m := &MQTT{} | ||||||
| assert.Equal(t, "12345.678", m.encode(12345.678), "large energy value with 3 decimals") | ||||||
| assert.Equal(t, "12345.6", m.encode(12345.6), "large energy value with 1 decimal") | ||||||
| assert.Equal(t, "12345", m.encode(12345.0), "large energy value without decimals") | ||||||
| assert.Equal(t, "0.123", m.encode(0.123456), "small value truncated to 3 decimals") | ||||||
|
||||||
| assert.Equal(t, "0.123", m.encode(0.123456), "small value truncated to 3 decimals") | |
| assert.Equal(t, "0.123", m.encode(0.123456), "small value rounded to 3 decimals") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Add tests that explicitly cover rounding at the 4th decimal place to ensure the behavior of the new format is locked in
Current tests only cover values that don’t stress rounding at the 4th decimal. Please add cases like 0.1239 → "0.124" and 1.9999 → "2" (or whatever is expected) to explicitly assert the rounding behavior of "%.3f" and guard against regressions from rounding to truncation.
Copilot
AI
Jan 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test coverage should include negative numbers to ensure the formatting works correctly for negative values (e.g., -12345.678, -0.123). While the implementation should handle them correctly, explicit test coverage would be valuable given that energy and power can be negative (e.g., during feed-in).
Copilot
AI
Jan 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding test coverage for very large numbers (e.g., values > 1e6) to verify that the formatting produces reasonable output. The old format used scientific notation for very large values, while the new format will produce fixed-point notation which could result in very long strings for extremely large values. However, this might be acceptable depending on the expected range of energy/power values in the application.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is MQTT and not some kind of frontend display: why do we even need to trim trailing digits? Lets keep it simple.