Convert USD Currency to Words

Quick Overview

Convert a non-negative US dollar amount given as a string into English words, with scale words up to Billion and an optional cents clause. It tests string parsing, splitting a number into three-digit groups, and careful handling of zero groups, teens and single-digit cents.

Convert USD Currency to Words

Company: Yext

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `amount` representing a non-negative US dollar value with up to two decimal places, convert it to its English word representation as a currency string. The dollar part is the dollar amount in words followed by `Dollars`. If there are non-zero cents, append ` and <words> Cents` (always plural). If the dollar portion is `0`, the dollar part is `Zero Dollars`. Use the scale words `Thousand`, `Million` and `Billion`, capitalize each word, and join words with single spaces (no hyphens and no inner "and"). ### Function Signature ```python def convert_to_words(amount: str) -> str: ``` ### Rules - Write the dollar amount in groups of three digits, highest group first, each followed by its scale word. A group that is `000` contributes no words. - `Dollars` and `Cents` are always plural, so `"1.01"` becomes `"One Dollars and One Cents"`. - When the cents are 0, the output ends at `Dollars`. Never write `and Zero Cents`. - If `amount` has no decimal point, treat cents as 0. - A single fractional digit means tens of cents: `"12.5"` is 12 dollars and 50 cents. - Leading zeros in either part are ignored. An empty dollar part (as in `".75"`) counts as 0, and a decimal point with no digits after it (as in `"12."`) means 0 cents. ### Constraints - `0 <= integer_part <= 999,999,999,999` - `0 <= cents <= 99` - `amount` contains only digits and at most one `.` - The fractional portion has at most 2 digits. ### Examples ```text Input: amount = "1234.56" Output: "One Thousand Two Hundred Thirty Four Dollars and Fifty Six Cents" ``` 1234 splits into the groups 1 and 234, which read as "One Thousand" and "Two Hundred Thirty Four". The 56 cents become "Fifty Six Cents". ```text Input: amount = "0" Output: "Zero Dollars" ``` The dollar portion is 0 and there is no decimal point, so cents are 0 and nothing follows `Dollars`. ```text Input: amount = "1000000.5" Output: "One Million Dollars and Fifty Cents" ``` The thousands group and the units group are both `000`, so they add no words. The single fractional digit 5 means 50 cents.

Overview: Convert a non-negative US dollar amount given as a string into English words, with scale words up to Billion and an optional cents clause. It tests string parsing, splitting a number into three-digit groups, and careful handling of zero groups, teens and single-digit cents.

Yext logo
Yext
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string amount representing a non-negative US dollar value with up to two decimal places, convert it to its English word representation as a currency string.

The dollar part is the dollar amount in words followed by Dollars. If there are non-zero cents, append and <words> Cents (always plural). If the dollar portion is 0, the dollar part is Zero Dollars. Use the scale words Thousand, Million and Billion, capitalize each word, and join words with single spaces (no hyphens and no inner "and").

Function Signature

def convert_to_words(amount: str) -> str:

Rules

  • Write the dollar amount in groups of three digits, highest group first, each followed by its scale word. A group that is 000 contributes no words.
  • Dollars and Cents are always plural, so "1.01" becomes "One Dollars and One Cents" .
  • When the cents are 0, the output ends at Dollars . Never write and Zero Cents .
  • If amount has no decimal point, treat cents as 0.
  • A single fractional digit means tens of cents: "12.5" is 12 dollars and 50 cents.
  • Leading zeros in either part are ignored. An empty dollar part (as in ".75" ) counts as 0, and a decimal point with no digits after it (as in "12." ) means 0 cents.

Constraints

  • 0 <= integer_part <= 999,999,999,999
  • 0 <= cents <= 99
  • amount contains only digits and at most one .
  • The fractional portion has at most 2 digits.

Examples

Input: amount = "1234.56"
Output: "One Thousand Two Hundred Thirty Four Dollars and Fifty Six Cents"

1234 splits into the groups 1 and 234, which read as "One Thousand" and "Two Hundred Thirty Four". The 56 cents become "Fifty Six Cents".

Input: amount = "0"
Output: "Zero Dollars"

The dollar portion is 0 and there is no decimal point, so cents are 0 and nothing follows Dollars.

Input: amount = "1000000.5"
Output: "One Million Dollars and Fifty Cents"

The thousands group and the units group are both 000, so they add no words. The single fractional digit 5 means 50 cents.