Number to a string

 BBS: Inland Empire Archive
Date: 03-13-93 (02:11)             Number: 384
From: TOM COAKLEY                  Refer#: NONE
  To: ALL                           Recvd: NO  
Subj: Number to a string             Conf: (2) Quik_Bas
I saw an earlier post where someone requested some quick basic code that
would convert a number (123.15) to a string (One Hundred Twenty Three
15/100)
I pulled the following code from a one of my programs.

========================================================================
SUB NUMWORD (NUM#, WORD$)

DIM ONEARRAY$(9)
DIM NUMARRAY$(9)
DIM TEENARRAY$(9)

ONEARRAY$(1) = "One "
ONEARRAY$(2) = "Two "
ONEARRAY$(3) = "Three "
ONEARRAY$(4) = "Four "
ONEARRAY$(5) = "Five "
ONEARRAY$(6) = "Six "
ONEARRAY$(7) = "Seven "
ONEARRAY$(8) = "Eight "
ONEARRAY$(9) = "Nine "
NUMARRAY$(1) = "TEEN LISTING"
NUMARRAY$(2) = "Twenty "
NUMARRAY$(3) = "Thirty "
NUMARRAY$(4) = "Forty "
NUMARRAY$(5) = "Fifty "
NUMARRAY$(6) = "Sixty "
NUMARRAY$(7) = "Seventy "
NUMARRAY$(8) = "Eighty "
NUMARRAY$(9) = "Ninety "
TEENARRAY$(1) = "Eleven "
TEENARRAY$(2) = "Twelve "
TEENARRAY$(3) = "Thirteen "
TEENARRAY$(4) = "Fourteen "
TEENARRAY$(5) = "Fifteen "
TEENARRAY$(6) = "Sixteen "
TEENARRAY$(7) = "Seventeen "
TEENARRAY$(8) = "Eighteen "
TEENARRAY$(9) = "Nineteen "


WORD$ = ""
NUM$ = STR$(NUM#)
NUM$ = RIGHT$(NUM$, LEN(NUM$) - 1)
NUMLEN = LEN(NUM$)
DECCHECK = INSTR(NUM$, ".")
IF DECCHECK > 0 THEN
   DECIMAL$ = RIGHT$(NUM$, NUMLEN - DECCHECK)
END IF
DENOMINATOR$ = "10"
FOR ZEROS = 1 TO LEN(DECIMAL$)
   DENOMINATOR$ = DENOMINATOR$ + "0"
NEXT ZEROS
DECIMAL$ = DECIMAL$ + "/" + DENOMINATOR$


NUM$ = LEFT$(NUM$, DECCHECK - 1)
NUMLEN = LEN(NUM$)


WHILE NUMLEN > 0
   NUM$ = RIGHT$(NUM$, NUMLEN)
   NUMCHECK$ = LEFT$(NUM$, 1)
   NUMCHECK = VAL(NUMCHECK$)
   ON NUMLEN GOSUB ONES, TENS, HUNDREDS, THOUSANDS,
TENTHOUSANDS, HUNDREDTHOUSA
   NUMLEN = NUMLEN - 1
WEND

WORD$ = WORD$ + DECIMAL$

EXIT SUB

HUNDREDTHOUSANDS:
  WORD$ = WORD$ + ONEARRAY$(NUMCHECK) + "Hundred "
RETURN
TENTHOUSANDS:
  IF NUMCHECK$ = "1" THEN
      GOSUB TEENS:
      WORD$ = WORD$ + "Thousand "
  ELSE
      WORD$ = WORD$ + NUMARRAY$(NUMCHECK)
  END IF
RETURN
THOUSANDS:
  WORD$ = WORD$ + ONEARRAY$(NUMCHECK) + "Thousand "
RETURN
HUNDREDS:
  WORD$ = WORD$ + ONEARRAY$(NUMCHECK) + "Hundred "
RETURN
TENS:
  IF NUMCHECK$ = "1" THEN
      GOSUB TEENS:
      WORD$ = WORD$
  ELSE
      WORD$ = WORD$ + NUMARRAY$(NUMCHECK)
  END IF
RETURN
ONES:
  WORD$ = WORD$ + ONEARRAY$(NUMCHECK)
RETURN
TEENS:
  NUMLEN = NUMLEN = NUMLEN - 1
  NUMCHECK$ = RIGHT$(NUM$, 1)
  NUMCHECK = VAL(NUMCHECK$)
  WORD$ = WORD$ + TEENARRAY$(NUMCHECK)
RETURN





END SUB
=====================================================================

I hope this solves your problem.
Tom



---
  SLMR 2.1a 
---
  SLMR 2.1a 
--- FidoPCB v1.4 beta
 * Origin: ASA CompuHelp  "We're A Step Ahead"  (614)476-4058 (1:226/600)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET