Constants & Formats Constants 2.3

advertisement
Constants & Formats
Constants 2.3
Constants are useful when the programmer declares a variable and it's value never changes. It is
best to declare constants in the general declarations area.
Constants are declared because it is easier to change a constant's value in one area. It is time
consuming to search for a variable and change its value in the locations where the variable has
been used.
The same rules for declaring variables also applies for constants.
(See the Variable Declaration section above.)
Syntax
Const <areadeclared_ConstantName> = <Value>
Example
Const f_CdnTax = 0.15
Const f_UserName = "Grade 11 Students"
Visual Basic also declare constants for your file properties in a module. eg. Global Const
Left_Justify = 0
The tax constant could be used in many of your programs dealing with currency.
Activity 2.3.1
Name and 5 instances where constants are necessary.
Use the proper syntax to declare the constants.
The Format Function 2.4
Functions in Visual Basic are predefined procedures that perform a specific task.
The Format Function
The format is used to improve the appearance of numbers in your application. The Format
Function formats any expression according to a set of string instructions you specify. If your
application included using currency for millions of dollars you could specify how the dollar
value should appear or you could use the predefined format function to display the dollar value.
When using the format function it is not necessary to code with the format function until the
formatted expressions are displayed.
Note: # is a place holder for a digit.
Pre-defined Formats
Format
Explanation
Currency
Displays a number with a dollar sign and two decimal places; if
appropriate displays a number with a thousand separator; negative
numbers are enclosed in parenthesis
Fixed
Displays a number with at least one digit to the left and two digits
to the right of the decimal point.
Standard
Displays a number with two decimal places; if appropriate,
displays a number with a thousand separator; negative numbers are
enclosed in parenthesis
Percent
Multiplies a number by 100 and displays the number with a
percent sign(%); displays two digits to the right of the decimal
point.
Scientific
Displays a value as a decimal number between 0 and 10 time the
appropriate power of 10.
Syntax
Format$(NumericExpression, FormatString$)
The $ sign is to distinguish that the format output will become a string. When a variable is a
variant the $ is not needed.
Note: the date function will be discussed below.
Examples
1. Format$(99, "Percent")
2. Format$(123000, "Currency")
3. txtNetPay.Text = Format$(m_NetPay, "Currency")
4. txtPlanetMass.Text = Format$(Mass, "Scientific")
5. txtTermMark.Text = Format$(FinalMark, "Percent")
6. lblPrice.Caption = Format(lblTotalPrice.Caption *100 *1.05, "Currency")
Activity 2.4.1
Write the code in a program for the examples above and note their output.
For example:
On a blank form create a command button and name it cmdDisplay with a caption Display. Type
in:
Print Format$(99, "Percent")
Run the program and click on Display.
Activity 2.4.2
The expressions below are manually/user defined. For the following expressions note the output
in your computer science binders.
What is the output????
Print Format$(987.654321, "###.##")
Print "The interest rate is " ; Format$(Payment, "####.##).
Assume:
Dim Payment as Integer
Payment = 4532.1
Print Format$(543.210, "###.###")
Print Format$(543.210, "000.000")
Print Format$(123456789.91, "#.#.##")
Print Format$(500000000, "#00,,"):"million yen"
Print Format$(81.234, "###.##%")
Print Format$(Amount, "$###.##").
Assume:
Dim f_Amount as Integer
Amount =12.3
Print Format$(456.120,"###.###")
Print Format$(1000000000000,"#.#.#.##")
Download