Skip to content

Add feature to convert numeric words to their number representation #6195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 18, 2025

Conversation

Sufi-san
Copy link
Contributor

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Features

  • Accurate Word-to-Number Conversion: Converts English word representations of numbers into their numeric form, supporting values from small fractions to trillions.

  • Handles Decimals and Negatives: Properly interprets decimal numbers (e.g., "three point one four" → 3.14) and negative values.

  • Robust Input Validation: Detects invalid phrases, incorrect formatting, and duplicate words (e.g., "one hundred hundred" → Invalid).

  • BigDecimal Support: Provides a method to directly convert word-based numbers into BigDecimal, ensuring precision for financial and scientific calculations.

  • Optimized for Readability: Modular design ensures clean, maintainable, and extensible code.

Example Usage

Here’s how you can use the WordsToNumber class in your Java application:

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        // Standard conversion
        System.out.println(WordsToNumber.convert("three hundred and forty two")); // Output: 342
        System.out.println(WordsToNumber.convert("negative five thousand two hundred and one")); // Output: -5201
        System.out.println(WordsToNumber.convert("one billion point zero zero zero zero one")); // Output: 1000000000.00001
        System.out.println(WordsToNumber.convert("negative zero point zero zero zero zero zero zero one")); // Output: -1E-7

        // BigDecimal conversion
        BigDecimal bd1 = WordsToNumber.convertToBigDecimal("one thousand five hundred and seventy five point zero three");
        System.out.println(bd1); // Output: 1575.03

        BigDecimal bd2 = WordsToNumber.convertToBigDecimal("negative zero point zero zero zero zero one");
        System.out.println(bd2); // Output: -0.00001

        BigDecimal bd3 = WordsToNumber.convertToBigDecimal("invalid input");
        System.out.println(bd3); // Output: null
    }
}

@codecov-commenter
Copy link

codecov-commenter commented Mar 14, 2025

Codecov Report

Attention: Patch coverage is 89.61749% with 19 lines in your changes missing coverage. Please review.

Project coverage is 73.75%. Comparing base (b44ecf7) to head (1803f18).

Files with missing lines Patch % Lines
...a/com/thealgorithms/conversions/WordsToNumber.java 89.61% 8 Missing and 11 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6195      +/-   ##
============================================
+ Coverage     73.59%   73.75%   +0.15%     
- Complexity     5228     5273      +45     
============================================
  Files           668      669       +1     
  Lines         18099    18282     +183     
  Branches       3496     3528      +32     
============================================
+ Hits          13320    13483     +163     
- Misses         4250     4258       +8     
- Partials        529      541      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DenizAltunkapan
Copy link
Collaborator

Your implementation is well-structured and effectively handles a variety of edge cases, including decimals, negative numbers, and large values.
However, the convert() method is quite complex, with multiple nested conditions and a high cyclomatic complexity. Breaking it into smaller helper methods would improve readability and maintainability. Using an Enum instead of HashMap for number mappings could enhance clarity and reduce lookup overhead. Additionally, error handling could be streamlined by using exceptions instead of tracking an error message string.
:)

@DenizAltunkapan
Copy link
Collaborator

Thank you for your patience. In my opinion, everything is working now. @siriak

@Sufi-san Sufi-san marked this pull request as ready for review March 15, 2025 10:11
Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@siriak siriak merged commit 4514887 into TheAlgorithms:master Mar 18, 2025
6 checks passed
@Sufi-san Sufi-san deleted the sufiyan-chougule branch March 18, 2025 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants