Float to string conversion
Filed under Amazon
Free read 1 of 3Split the float into integer and fractional components.
The question
Convert a floating-point number to its string representation without using built-in conversion functions like str() or sprintf().
Constraints
- Must handle negative numbers.
- Must handle decimal points.
- Precision requirements should be defined.
Examples
- Input
- 123.45
- Output
- "123.45"
- Why
- Manual extraction of integer and fractional parts.
Approach
Split the float into integer and fractional components.
Convert the integer part by repeatedly taking modulo 10 and mapping to characters.
Multiply the fractional part by 10 repeatedly to extract digits until the desired precision is reached.
Concatenate the parts with a decimal point and handle the sign.
Tips
- What they are testing: Test with 0.0, negative numbers, and numbers with trailing zeros (e.g., 1.50).
- Common mistake: Precision loss when multiplying the fractional part by powers of 10.
Related Amazon questions
Collected from public candidate posts and coding forums. Updated Nov 2026.