frappe / books

Free Accounting Software
https://frappe.io/books
GNU Affero General Public License v3.0
2.68k stars 614 forks source link

Amount in Words for Cheque Printing #766

Open sirwilliam15 opened 8 months ago

sirwilliam15 commented 8 months ago

Request to add a field for payment amount in words for creating a cheque print template.

Ex. Amount: $1,024.00 Amount in Words: One Thousand and Twenty Four and 0/100

mildred commented 7 months ago

Did you try custom fields, would that work for you?

01111010t commented 5 months ago

maybe implementing a function similar to the below would work?

function toSpelledOutNumber(number: number): string {
  // Ensure two decimal places
  const formattedNumber = number.toFixed(2);

  // Split integer and decimal parts
  const [integerPart, decimalPart] = formattedNumber.split(".");

  // Constants for number names
  const ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
  const teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
  const tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];

  // Function to convert an integer to words
  function convertInteger(num: number) {
    if (num === 0) {
      return "";
    }

    if (num < 10) {
      return ones[num];
    }

    if (num < 20) {
      return teens[num - 10];
    }

    const tensDigit = Math.floor(num / 10);
    const onesDigit = num % 10;

    if (onesDigit === 0) {
      return tens[tensDigit];
    }

    return `${tens[tensDigit]}-${ones[onesDigit]}`;
  }

  // Spell out the integer part
  let spelledOutInteger = "";
  for (const group of integerPart.match(/.{1,3}/g) || []) {
    const groupValue = parseInt(group);
    spelledOutInteger += convertInteger(groupValue) + " hundred ";
  }

  spelledOutInteger = spelledOutInteger.trim();

  // Spell out the decimal part
  let spelledOutDecimal = "";
  const decimalCents = parseInt(decimalPart);
  if (decimalCents !== 0) {
    spelledOutDecimal = "and " + convertInteger(decimalCents) + " cents";
  }

  // Combine the parts
  return spelledOutInteger + spelledOutDecimal;
}