Numbers to Words API

The Numbers to Words API provides a simple service for converting numerical values into their equivalent English words.

Base URL

"https://openapi-idk8.onrender.com/"

Convert Numbers to Words Endpoint

GET `/number`

Description: Converts the provided numbers to their equivalent English words. This can be particularly useful for applications that require numeric data to be displayed in a more human-readable format, such as financial documents, educational tools, or accessibility features.

Query Parameters

  • numbers (required): A space-separated list of numbers to be converted.

Sample Request

GET `https://openapi-idk8.onrender.com/number?numbers=123 456 789`

Sample Response

{
  "api_info": {
    "api_name": "Numbers to Words",
    "description": "The Numbers to Words API provides a simple service for converting numerical values into their equivalent English words.",
    "author": "OpenAPI"
  },
  "original": "123 456 789",
  "converted": "One Hundred Twenty Three, Four Hundred Fifty Six, Seven Hundred Eighty Nine"
}

Implementation Details

The API uses a helper function to handle the conversion of numbers to words:

function numberToWords(num) {
    const belowTwenty = [
        'Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten',
        'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'
    ];
    const tens = [
        '', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'
    ];
    const thousands = [
        '', 'Thousand', 'Million', 'Billion', 'Trillion'
    ];

    if (num === 0) return belowTwenty[0];

    function helper(n) {
        if (n < 20) return belowTwenty[n];
        if (n < 100) return tens[Math.floor(n / 10)] + (n % 10 > 0 ? ' ' + belowTwenty[n % 10] : '');
        if (n < 1000) return belowTwenty[Math.floor(n / 100)] + ' Hundred' + (n % 100 > 0 ? ' ' + helper(n % 100) : '');
        for (let i = 0; i < thousands.length; i++) {
            const unit = 1000 ** (i + 1);
            if (n < unit) return helper(Math.floor(n / (unit / 1000))) + ' ' + thousands[i] + (n % (unit / 1000) > 0 ? ' ' + helper(n % (unit / 1000)) : '');
        }
    }

    return helper(num);
}

Usage Example

To use the API, make a GET request to the `/number` endpoint with the required query parameter:

fetch('https://openapi-idk8.onrender.com/number?numbers=123 456 789')
    .then(response => response.json())
    .then(data => console.log(data));

Error Handling

400 Bad Request: Returned when the required query parameter is missing.

{
  "error": "The \"numbers\" query parameter is required."
}