Using Decimal Types
What are the data types that you can use? Decimal Number: Single -Is used for decimal values that will not exceed six or seven digits. Double - Is used for decimal values with more than six or seven digits. Currency - Is used when working with dollars and cents.
Using the Format Function! Allows you to apply custom formatting to a number before displaying the value. Use it for: –Decimal values –Phone numbers –Currency $$$$$ –Social security numbers –ID numbers –Dates –Time
How? You specify the format you want using special symbols. For example: Display to appear as $1, strAmount = Format( , “$#,###.00”)
Formatting Symbols SymbolDescription 0 The 0 symbol causes a digit to appear in the space. If the data has no value for that digit, a zero appears. # Is similar to the 0 symbol. The difference is that nothing appears in the space if the number being formatted does not require that digit to be used.. The period is used to specify where you want the decimal point to appear in the format.
SymbolDescription, By placing commas in the format it will appear in the format. % The percent sign causes a number to be multiplied by 100 and a percent sign to be placed at the end of the number.
Examples! CodeResult Format( , “ ” Format( , “######.000”) Format( , “######.###”) Format( , “###,###.##”) 12,345,67 Fomat( , “$###,###.##”) $12, Format(0.89, “##%”) 89%
Let’s look at a program. Open Visual Basic Open the Dog Years program from the c:\drive. Double click the Calculate age in months. Enter the following code: ‘Display Number of Months Old lblOutputMonths.Caption = “You are” & intMonths & “months old.” lblOUtputMonths.Visible = True
Double click the Calculate age in months. Enter the following code: ‘Display Instructions lblInstructions.Visible = True Double click the Dog button. Enter the following code: Dim strYears As String Dim sngYears As Single ‘Calculate Dog Years sngYears = (7 * intMonths) / 12 ‘Hide Instructions lblInstructions.Visible = False
‘Display Results strYears = Format(sngYears, “###.0”) lblOutputAnimalYears.Caption = “In dog years, you are “ & strYears & “years old.” lblOutputAnimalYears.Visible = True
Close the code window and run the program. Enter 15 in the Age in years Enter 4 in the Months since last birthday Click the Calculate age in months and dog buttons. What happens?