330. Floating-Point Number to String
Floating-Point Number to String
Implement ftoa to convert a floating-point number into a string without using standard number-to-string conversion functions.
The result must contain exactly precision digits after the decimal point. Extra fractional digits must be discarded without rounding, and missing fractional digits must be replaced with trailing zeros.

Method Signature

String ftoa(float num, int precision)
  • num is the finite floating-point number to convert.
  • precision is the exact number of digits required after the decimal point.
  • Returns the manually constructed string representation of num.

Conversion Rules

  • Use the actual value stored in num. Fractional information that cannot be represented by the float type cannot be recovered.
  • Discard digits beyond precision by truncating toward zero. Do not round the number.
  • The result must contain exactly precision fractional digits.
  • Add trailing zeros when the number has fewer fractional digits than required.
  • When precision is 0, do not include a decimal point.
  • Use a period (.) as the decimal separator.
  • Do not use scientific notation.
  • Do not add a plus sign to positive numbers.
  • The integer part must contain at least one digit. Values between -1 and 1 must therefore have 0 before the decimal point.
  • Include a minus sign when num < 0, even if truncation makes the displayed magnitude zero.
  • Treat both 0.0f and -0.0f as zero and do not include a minus sign for either value.

Restrictions

  • Do not use Float.toString, String.valueOf, String.format, Formatter, DecimalFormat, or equivalent conversion utilities.
  • Do not convert the number implicitly by concatenating it with a string.
  • Arithmetic operations, loops, integral casts, character operations, and StringBuilder may be used.

Constraints

  • -1,000,000,000.0f ≤ num ≤ 1,000,000,000.0f
  • 0 ≤ precision ≤ 6
  • num is neither infinity nor NaN.
  • The scaled value abs(num) * 10^precision fits in a long.

Examples

Example 1

ftoa(num = 7.896f, precision = 2)
Output: "7.89"
Only the first two fractional digits are retained. The result is not rounded.

Example 2

ftoa(num = -48.729f, precision = 1)
Output: "-48.7"
Truncation toward zero removes all fractional digits after the first.

Example 3

ftoa(num = 25.5f, precision = 4)
Output: "25.5000"
Three trailing zeros are added to produce exactly four fractional digits.

Example 4

ftoa(num = 93.875f, precision = 0)
Output: "93"
The fractional part and decimal point are omitted when the precision is zero.

Example 5

ftoa(num = 0.0625f, precision = 3)
Output: "0.062"
The integer part is written as zero, and the remaining fractional digit is discarded.

Example 6

ftoa(num = -0.004f, precision = 2)
Output: "-0.00"
The original value is negative, so its minus sign is retained even though its displayed magnitude becomes zero after truncation.

Example 7

ftoa(num = -0.0f, precision = 3)
Output: "0.000"
Negative zero is treated as zero and is displayed without a minus sign.


Please use Laptop/Desktop or any other large screen to add/edit code.