Post Reply 
how to pronounce numbers
06-25-2018, 03:27 AM (This post was last modified: 06-25-2018 03:33 AM by DavidM.)
Post: #26
RE: how to pronounce numbers
(06-24-2018 06:36 PM)Gerson W. Barbosa Wrote:  BTW, has anyone here also written a program to fill checks (or cheques), but in English?

I am interested in the full numbers routine. Who still fills checks these days?

This is a messy-and-dirty RPL transcription of a routine I once used to create the text of the "body amount" of a check-printing application. I didn't bother including the cents part of the routine, as it merely generated "and xx/100" (xx being the two digits after the radix mark).

Apologies in advance for this -- I haven't spent any time trying to optimize it or make it more RPL-friendly. It's ugly, but I believe this is a faithful rendition of the original algorithm.

Although not needed for checks, it does handle zero, negative numbers, and larger amounts than would normally be used on a check Smile (x<1E45). The original also capitalized the first letter, which I didn't bother to include in this transcription.

Code:
\<<
  @ digits
  { "one" "two" "three" "four" "five" "six" "seven"
    "eight" "nine" }

  @ teens
  { "ten" "eleven" "twelve" "thirteen" "fourteen"
    "fifteen" "sixteen" "seventeen" "eighteen"
    "nineteen" }

  @ tens
  { "twenty" "thirty" "forty" "fifty" "sixty" "seventy"
    "eighty" "ninety" }

  @ magnitude
  { "thousand" "million" "billion" "trillion" "quadrillion"
    "quintillion" "sextillion" "septillion" "octillion"
    "nonillion" "decillion" "undecillion" "duodecillion"
    "tredecillion" }

  @ subroutine to append string with preceding space if appropriate
  \<<
    OVER SIZE { " " SWAP + } IFT +
  \>>

  \-> digits teens tens magnitude addstr
  \<<
    @ discard fractional value if real (for RPL)
    IF
      DUP TYPE NOT
    THEN
      IP
    END

    @ translate to zero if appropriate, otherwise proceed
    IF
      DUP NOT
    THEN
      DROP "zero"
    ELSE
      @ retain sign
      DUP SIGN SWAP ABS

      @ pre-load stack with magnitude, initial string
      0. "" ROT

      @ process triples until done
      WHILE
        DUP
      REPEAT
        @ get next triple
        1000 IDIV2

        @ split triple into hundreds and remainder
        100 IDIV2

        @ convert hundreds of this triple
        SWAP
        IF
          DUP
        THEN
          digits SWAP GET
          " hundred" +
        ELSE
          DROP ""
        END

        @ convert remainder digits of this triple
        SWAP
        IF
          DUP
        THEN
          CASE
            10 OVER > THEN
              digits SWAP GET addstr EVAL
            END
            20 OVER > THEN
              teens SWAP 9. - GET addstr EVAL
            END
            10 IDIV2 SWAP
            tens SWAP 1. - GET ROT SWAP addstr EVAL
            SWAP
            IF
              DUP
            THEN
              digits SWAP GET
              "-" SWAP + +
            ELSE
              DROP
            END
          END
        ELSE
          DROP
        END

        @ add magnitude if appropriate
        4. ROLL
        IF
          OVER SIZE OVER AND
        THEN
          " " magnitude PICK3 GET +
          ROT SWAP + SWAP
        END
        1. + 4. ROLLD
        ROT

        @ accumulate new triple into final string
        IF
          OVER SIZE
          OVER SIZE AND
        THEN
          " " SWAP +
        END
        +
        SWAP
      END

      @ drop temporary stack values
      ROT DROP2

      @ prepend "negative" if appropriate
      IF
        SWAP 0. <
      THEN
        "negative " SWAP +
      END
    END
  \>>
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
how to pronounce numbers - Don Shepherd - 06-23-2018, 10:48 AM
RE: how to pronounce numbers - Paul Dale - 06-23-2018, 11:05 AM
RE: how to pronounce numbers - grsbanks - 06-23-2018, 12:16 PM
RE: how to pronounce numbers - ttw - 06-23-2018, 12:58 PM
RE: how to pronounce numbers - ttw - 06-23-2018, 07:43 PM
RE: how to pronounce numbers - grsbanks - 06-23-2018, 01:22 PM
RE: how to pronounce numbers - Wes Loewer - 06-23-2018, 01:49 PM
RE: how to pronounce numbers - Wes Loewer - 06-23-2018, 03:01 PM
RE: how to pronounce numbers - peacecalc - 06-23-2018, 03:12 PM
RE: how to pronounce numbers - grsbanks - 06-23-2018, 07:27 PM
RE: how to pronounce numbers - Dieter - 06-25-2018, 11:04 AM
RE: how to pronounce numbers - Zaphod - 06-23-2018, 09:19 PM
RE: how to pronounce numbers - ttw - 06-23-2018, 07:45 PM
RE: how to pronounce numbers - grsbanks - 06-23-2018, 07:50 PM
RE: how to pronounce numbers - franz.b - 06-23-2018, 08:11 PM
RE: how to pronounce numbers - Wes Loewer - 06-24-2018, 01:08 AM
RE: how to pronounce numbers - Zaphod - 06-24-2018, 10:49 AM
RE: how to pronounce numbers - DavidM - 06-25-2018 03:27 AM
RE: how to pronounce numbers - grsbanks - 06-25-2018, 12:55 PM
RE: how to pronounce numbers - DavidM - 06-25-2018, 01:22 PM
RE: how to pronounce numbers - Zaphod - 06-24-2018, 07:09 PM
RE: how to pronounce numbers - BartDB - 06-24-2018, 07:53 PM
RE: how to pronounce numbers - ttw - 06-24-2018, 11:20 PM
RE: how to pronounce numbers - grsbanks - 06-25-2018, 10:34 AM
RE: how to pronounce numbers - rprosperi - 06-25-2018, 01:25 PM
RE: how to pronounce numbers - rprosperi - 06-25-2018, 10:23 PM
RE: how to pronounce numbers - Paul Dale - 06-25-2018, 06:47 AM
RE: how to pronounce numbers - rncgray - 06-25-2018, 07:42 AM
RE: how to pronounce numbers - 3298 - 06-25-2018, 03:30 PM
RE: how to pronounce numbers - grsbanks - 06-25-2018, 03:33 PM
RE: how to pronounce numbers - grsbanks - 06-26-2018, 07:08 AM
RE: how to pronounce numbers - Dieter - 06-27-2018, 07:05 AM
RE: how to pronounce numbers - grsbanks - 06-27-2018, 07:31 AM
RE: how to pronounce numbers - rprosperi - 06-27-2018, 01:21 PM
RE: how to pronounce numbers - brickviking - 06-26-2018, 08:54 AM
RE: how to pronounce numbers - rprosperi - 06-26-2018, 01:23 PM
RE: how to pronounce numbers - ttw - 06-26-2018, 05:45 AM
RE: how to pronounce numbers - rncgray - 06-26-2018, 07:29 AM
RE: how to pronounce numbers - grsbanks - 06-26-2018, 07:50 AM
RE: how to pronounce numbers - grsbanks - 06-26-2018, 12:19 PM



User(s) browsing this thread: 5 Guest(s)