FastPrepConvert a Float to a String
Problems/Convert a Float to a String

Convert a Float to a String

Microsoft logoMicrosoft● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a review of a Microsoft software engineering internship. The task as reported is to turn a float into text without library helpers. It was asked in the last technical round, a final interview with a single interviewer. That round also had networking and project questions.

The review gives no output format, precision or rounding rule. Here you get the number and how many digits to show after the decimal point. You round to the nearest value, and the fraction is padded with zeros to exactly that many digits. A value below 1 keeps its leading 0, a zero result has no minus sign, and 0 digits means no decimal point. Values are kept away from exact halfway points so floating-point error cannot change an answer. The no-built-ins rule is part of the statement, but the tests cannot check it.

Our version is about 60% the same as the reported question.

Write your own conversion of a floating-point number to text. Given a number x and an integer digits, return x written in decimal with exactly digits digits after the decimal point, rounded to the nearest such value.

  • Write at least one digit before the decimal point, for example 0.25, not .25.
  • Pad the fraction with zeros to exactly digits digits, for example 1.050.
  • If digits is 0, write no decimal point.
  • Write a minus sign for a negative result, but if the rounded result is zero, write it without a minus sign.

Do not use your language's built-in number formatting or number-to-string functions. The tests cannot check this part, but it is the point of the question.

Function

formatNumber(x: double, digits: int) → String

Examples

Example 1

x = 3.14159digits = 2return = "3.14"

With 2 digits after the point, 3.14159 lies between 3.14 and 3.15 and is closer to 3.14, so the answer is 3.14.

Example 2

x = 0.999digits = 2return = "1.00"

0.999 is closer to 1.00 than to 0.99. Rounding up carries into the whole-number part, so the answer is 1.00, not 0.100.

Example 3

x = -0.004digits = 2return = "0.00"

-0.004 rounds to zero with 2 digits. A result of zero is written without a minus sign: 0.00.

Example 4

x = 1234.5678digits = 0return = "1235"

With 0 digits after the point, the number is rounded to the nearest whole number, 1235, and no decimal point is written.

Constraints

  • -10^6 <= x <= 10^6
  • 0 <= digits <= 6
  • The value |x| * 10^digits is never within 0.001 of a number ending in .5, so there are no near-ties to round.

More Microsoft Onsite Interview questions

  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
  • Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
  • Iterate Canonical IPv6 AddressesOpenAI · ONSITE INTERVIEWOpenAI interview questions
  • Nth Staged License PlateGoogle · ONSITE INTERVIEWGoogle interview questions
  • Bit at an Index After Repeated Binary ExpansionAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Text Embedding and Vector SearchNike, Inc. · PHONE SCREEN · ONSITE INTERVIEWNike, Inc. interview questions
  • Fraction to Recurring DecimalGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Rank Bipedal Dinosaurs by SpeedMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Every Result of Grouping an ExpressionC3.ai · ONSITE INTERVIEWC3.ai interview questions
  • Largest Minus Smallest Value of a Parenthesized ExpressionNutanix · ONSITE INTERVIEWNutanix interview questions
  • String to Integer with ClampingAMD · ONSITE INTERVIEWAMD interview questions

Interview reports that mention this question

  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern Interviews: 244 Experiences (Part 1 of 6)

Microsoft interview experiences

  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
  • Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public String formatNumber(double x, int digits) {
        // Write your code here.
    }
}
Checking account…