FAQ
TL;DR: Need to parse UART digits and display floats cleanly? Use "String(A, 2)" to show 2 decimal places; trim trailing zeros if needed. [Elektroda, witoldwitoldowicz, post #17243050]
Why it matters: This FAQ helps Arduino users turn UART ASCII readings into ints/floats and display them without unwanted zeros.
Quick Facts
- Convert 3 ASCII digits to a number: copy them into a String, then call toInt(). [Elektroda, witoldwitoldowicz, post #17241848]
- Fixed precision printing: String(value, decimalPlaces) formats a float to N decimals. [Elektroda, witoldwitoldowicz, post #17243029]
- Removing trailing zeros: loop while the last char is '0' after a '.' and delete it. [Elektroda, witoldwitoldowicz, post #17243103]
- Payload example: 9 characters total, 3 each for voltage, current, and power. [Elektroda, Maryush, post #17241759]
- Strings are zero-indexed; the last character sits at length-1. [Elektroda, krzbor, post #17243933]
How do I convert three ASCII digits from a UART char array into an int in Arduino?
Copy the three characters into a temporary String, then call toInt() to get a number. This approach is fast and readable. It matches fixed-width fields like 3-digit values. 3-step How-To:
- String s = String(buf + start).substring(0, 3)
- long n = s.toInt()
- Use n as your integer
This suits 3-digit slices for voltage/current/power. [Elektroda, witoldwitoldowicz, post #17241848]
What’s the quickest way to display a float with exactly two decimal places?
Use the String constructor with precision: String(A, 2). It converts the float to text with two decimals. This is ideal for UI output and logs. You can then print or display the resulting String. "String text= String(A, 2);" demonstrates the exact call. [Elektroda, witoldwitoldowicz, post #17243050]
How can I remove trailing zeros so 27.500 prints as 27.5?
Convert the value to a String, check for '.', then remove trailing '0' characters. Keep looping until the last character is no longer '0'. Add a final check if you want to remove a trailing '.' too. "while( as long as the last character is '0' )" describes the core idea. [Elektroda, witoldwitoldowicz, post #17243203]
Why do examples use length-1 to access the last character of a String?
String indices start at 0, but length gives a count starting at 1. For "abc", indices are 0:'a', 1:'b', 2:'c'. The last index is therefore length-1. Use that index when trimming or peeking the final character. [Elektroda, krzbor, post #17243933]
How do I split a 9-character UART payload into three values?
Treat the payload as three fixed fields. Slice [0..2] for voltage, [3..5] for current, and [6..8] for power. Convert each 3-character slice using String(...).toInt(). This works because the packet length is 9 (3 characters per value). [Elektroda, Maryush, post #17241759]
What happens if String.toInt() fails to find digits?
String.toInt() returns 0 when no valid conversion can be performed. This is a common failure signal, so 0 can mean either a real zero or an error. Validate your slice contains digits (and an optional leading '-') before conversion. Or use a sentinel or bounds check to distinguish cases. [String.toInt()]
Can I limit trimming to zeros only when a decimal point exists?
Yes. First confirm the String contains '.' using indexOf('.'). Then loop removing trailing '0' characters. This prevents deleting zeros from integer strings. Optionally, if the last char becomes '.', remove it too. [Elektroda, witoldwitoldowicz, post #17243103]
Is there a one-liner to print two decimal places without building a String?
Yes. Use Serial.print(A, 2) or Serial.println(A, 2) to print a float with two decimals. This avoids intermediate Strings and reduces memory churn. It’s handy for quick debugging or streaming over USB/UART. [Serial.print()]
How should I slice substrings correctly when grabbing three digits?
Use substring(start, end) with end exclusive. For characters at positions 3, 4, 5 call substring(3, 6). Then convert the result with toInt(). Remember zero-based indexing when computing start and end. [StringObject]
My sensor uses a decimal comma. How can I parse that on Arduino?
Replace commas with dots before converting. For example: s.replace(',', '.'); then parse with toFloat() or manage the integer and fractional parts. This normalization step makes parsing consistent across locales. [StringObject]
Should I use Serial.parseInt() instead of manual slicing?
Use Serial.parseInt() when numbers are separated by non-digits or whitespace. It scans the stream for the next integer automatically. Manual slicing is better when your packet is fixed-width, like 3-digit fields. Choose based on your input format. [Serial.parseInt()]
Will building Strings for parsing impact memory on Arduino Uno?
The Uno has 2 KB of SRAM, so frequent or large String operations can consume memory quickly. Prefer short-lived, fixed-size operations when possible. For stable streams with fixed-width fields, slice and convert promptly. Monitor free memory during development. [Arduino Uno Rev3]