Convert a Float to a String
Updated
Problem statement
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
digitsdigits, for example1.050. - If
digitsis0, 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) → StringExamples
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^60 <= digits <= 6- The value
|x| * 10^digitsis never within0.001of 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