home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vb6_sr_1 / convert.frm (.txt) next >
Encoding:
Visual Basic Form  |  1999-02-25  |  6.3 KB  |  193 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   1560
  5.    ClientLeft      =   60
  6.    ClientTop       =   375
  7.    ClientWidth     =   6840
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   1560
  10.    ScaleWidth      =   6840
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.CommandButton Command1 
  13.       Caption         =   "Convert Numeric Dollar Amount to Text "
  14.       Height          =   375
  15.       Left            =   120
  16.       TabIndex        =   2
  17.       Top             =   1080
  18.       Width           =   6615
  19.    End
  20.    Begin VB.TextBox Text2 
  21.       Height          =   495
  22.       Left            =   1440
  23.       TabIndex        =   1
  24.       Top             =   480
  25.       Width           =   5295
  26.    End
  27.    Begin VB.TextBox Text1 
  28.       Height          =   495
  29.       Left            =   120
  30.       TabIndex        =   0
  31.       Top             =   480
  32.       Width           =   1215
  33.    End
  34.    Begin VB.Label Label1 
  35.       Caption         =   $"Convert.frx":0000
  36.       Height          =   615
  37.       Left            =   120
  38.       TabIndex        =   3
  39.       Top             =   0
  40.       Width           =   6615
  41.    End
  42. Attribute VB_Name = "Form1"
  43. Attribute VB_GlobalNameSpace = False
  44. Attribute VB_Creatable = False
  45. Attribute VB_PredeclaredId = True
  46. Attribute VB_Exposed = False
  47. '*****************************************************
  48. 'Project: Convert.VBP
  49. 'Author: Burt Abreu
  50. 'Date: 21 January 1999
  51. '*****************************************************
  52. 'Comments: This code will convert a numeric dollar
  53. 'amount into text for use in such applications as
  54. 'printing a check.
  55. 'This project was built using a Tip and code sent to
  56. 'the VB4UandME site by PineryJim@aol.com. It is posted
  57. 'here after the webmaster at VB4UandMe sent me this
  58. 'and a few other code items when he shut down the site.
  59. '-----------------------------------------------------
  60. 'Bug Fixes & Modifications
  61. '-----------------------------------------------------
  62. 'Author     Date      Comments
  63. '-----------------------------------------------------
  64. 'Burt Abreu 01/21/99
  65. '*-Converted the original code sample into this project.
  66. '*-Added commenting and renamed some controls and
  67. '  variables.
  68. '*-Removed GoSub and moved ParseChunk routine to sub.
  69. '*-Added validation to insure that a value was entered in
  70. '  the text box.
  71. '*****************************************************
  72. 'You may use this code freely in your own projects
  73. 'with the following conditions:
  74. 'No warranty is provided or should be assumed as to
  75. 'suitability of this code for any particular purpose
  76. 'and the author and webmaster of this site have no
  77. 'liablity. All code is provided "AS-IS" -use at your
  78. 'own risk.
  79. 'If you wish to post the code to another site or make
  80. 'it a part of any product, freeware or otherwise, whose
  81. 'main feature is the code itself then you must first
  82. 'request permission by contacting Burt Abreu at the
  83. 'Visual Basic Explorer site http://www.vbexplorer.com
  84. '*******************************************************
  85. Option Explicit
  86. 'Set up two arrays to hold string values we
  87. 'will use to convert numbers to words
  88. Dim BigOnes(9) As String
  89. Dim SmallOnes(19) As String
  90. 'Declare variables
  91. Dim Dollars As String
  92. Dim Cents As String
  93. Dim Words As String
  94. Dim Chunk As String
  95. Dim Digits As Integer
  96. Dim LeftDigit As Integer
  97. Dim RightDigit As Integer
  98. Public Sub ParseChunk()
  99.     Digits = Mid(Chunk, 1, 1)
  100.     If Digits > 0 Then
  101.         Words = Words & " " & SmallOnes(Digits) & " Hundred"
  102.     End If
  103.     Digits = Mid(Chunk, 2, 2)
  104.     If Digits > 19 Then
  105.         LeftDigit = Mid(Chunk, 2, 1)
  106.         RightDigit = Mid(Chunk, 3, 1)
  107.         Words = Words & " " & BigOnes(LeftDigit)
  108.         If RightDigit > 0 Then
  109.             Words = Words & " " & SmallOnes(RightDigit)
  110.         End If
  111.     Else
  112.         If Digits > 0 Then
  113.             Words = Words & " " & SmallOnes(Digits)
  114.         End If
  115.     End If
  116. End Sub
  117. Private Sub Command1_Click()
  118. 'format the incoming number to guarantee six digits
  119. 'to the left of the decimal point and two to the right
  120. 'and then separate the dollars from the cents
  121.     Text1.Text = Format(Text1.Text, "000000.00")
  122.     Dollars = Left(Text1.Text, 6)
  123.     Cents = Right(Text1.Text, 2)
  124.     Words = ""
  125. '------------------------------------------------------------
  126. 'check to make sure incoming number is not too large. Since
  127. 'we are reading in the first 6 characters in the statement
  128. 'above "Dollars = Left(Text1.Text, 6)" this isn't likely but
  129. 'this would be a good place to limit dollar amounts so I left
  130. 'it here. For instance, you might replace the hard coded
  131. '999999 with a variable "MaxDollars" if you wanted to
  132. '------------------------------------------------------------
  133.     If Dollars > 999999 Then
  134.         Text2.Text = "Dollar amount is too large"
  135.     ElseIf Dollars = "" Then
  136.         Text2.Text = "Please enter an amount"
  137.         Exit Sub
  138.     End If
  139. 'separate the dollars into chunks
  140.     If Dollars = 0 Then
  141.         Words = "Zero"
  142.     Else
  143. 'first do the thousands
  144.         Chunk = Left(Dollars, 3)
  145.         If Chunk > 0 Then
  146.             ParseChunk
  147.             Words = Words & " Thousand"
  148.         End If
  149.         
  150. 'do the rest of the dollars
  151.         Chunk = Right(Dollars, 3)
  152.         If Chunk > 0 Then
  153.             ParseChunk
  154.         End If
  155.     End If
  156. 'concatenate the cents and display
  157.     If Cents = 0 Then Cents = "No"
  158.     Words = Words & " and " & Cents & "/100"
  159.     Text2.Text = Words
  160.     Exit Sub
  161. End Sub
  162. Private Sub Form_Load()
  163. 'Populate the arrays
  164.     BigOnes(1) = "Ten"
  165.     BigOnes(2) = "Twenty"
  166.     BigOnes(3) = "Thirty"
  167.     BigOnes(4) = "Forty"
  168.     BigOnes(5) = "Fifty"
  169.     BigOnes(6) = "Sixty"
  170.     BigOnes(7) = "Seventy"
  171.     BigOnes(8) = "Eighty"
  172.     BigOnes(9) = "Ninety"
  173.     SmallOnes(1) = "One"
  174.     SmallOnes(2) = "Two"
  175.     SmallOnes(3) = "Three"
  176.     SmallOnes(4) = "Four"
  177.     SmallOnes(5) = "Five"
  178.     SmallOnes(6) = "Six"
  179.     SmallOnes(7) = "Seven"
  180.     SmallOnes(8) = "Eight"
  181.     SmallOnes(9) = "Nine"
  182.     SmallOnes(10) = "Ten"
  183.     SmallOnes(11) = "Eleven"
  184.     SmallOnes(12) = "Twelve"
  185.     SmallOnes(13) = "Thirteen"
  186.     SmallOnes(14) = "Fourteen"
  187.     SmallOnes(15) = "Fifteen"
  188.     SmallOnes(16) = "Sixteen"
  189.     SmallOnes(17) = "Seventeen"
  190.     SmallOnes(18) = "Eighteen"
  191.     SmallOnes(19) = "Nineteen"
  192. End Sub
  193.