Topic 3
Topic 3
LOCAL
DECLARATION
Variable Lifetime
Lifetime – the period of time the variable
exist
Local variable exist each time the sub
procedure executed, and disappear when
the procedures finishes
Module-level variable lifetime is the entire
time the form is loaded
Arithmetic Operations
Standard operators: ^, *, /, +, -
Operator precedence determines how an
equations is evaluated:
Parentheses
Exponentiation
Multiplication or Division (from left to right)
Addition or Subtraction (from left to right)
/ 15/2 7.5
\ 12\5 2
36\5 7
MOD
150/60
150 Mod 60 = 30 =2
=Bal 30
3+2x4-6 = 5
20-5+3 = 18
15-22+6 = 17
2+(3-1)+7 = 11
Converting Strings to a
Numeric Data Type
Values of text boxes and labels are actually
strings od text characters.
They should not be used directly in calculations,
just in case they are not numbers.
Identifiers=Integer .Parse(Object.Property)
intQuantity = Integer.Parse(txtQuantity.Text)
Identifiers=Decimal.Parse(Object.Property)
Parse Function (cont.)
How does it works?
When the Parse function converts an
argument to numeric, it begins at the
argument’s left most character
If that character is a numeric digit, decimal
point or sign, Parse converts the character to
numeric and move to the next character. As
soon as a non-numeric character is found,
the operation stops.
Example – Values returned by
Parse()
Contents of Argument Numeric value returned by the
Parse()
(blank) 0
123.45 123.45
$100 0
1,000 1
A123 0
123A 123
4B5 4
-123 -123
+123 123
12.34.8 12.34
Formatting Data
Formatting data: to format data for display (on
the printer or on the screen)
To display output:
Object.Property = Value.ToString(Format Data)
txtExtendedPrice.text=decExtendedPrice.ToString(
“C”)
lblExtendedPrice.Text =
'Calculate the extended price,discount and discounted price
decExtendedPrice = intQuantity * decPrice
decDiscount = decDiscountRate * decExtendedPrice
decDiscountedPrice = decExtendedPrice ‐ decDiscount
RM1,000.25
Format Number Function (“N”)
Thevalue will displays with commas and
two digits to the right of the decimal point.
Simple form:
.ToString(“N”)
.ToString(“N3”)
Example:
txtSum.text = decSum.ToString(”N”) 1,000.25
txtSum.text = decSum.ToString(”N3”) 1,000.253
Format Fixed-point Function (“F”)
Format as a string of:
numeric digits,
No commas,
2 decimal places, and
A minus sign at the left for negative values.
Simple form:
.ToString(“F”)
.ToString(“F3”)
Example:
1000.25
txtSum.text = decSum.ToString(”F”)
txtSum.text = decSum.ToString(”F3”) 1000.253
Format Digits Function (“D”)
Use only for integer data types.
Format with a left minus sign for negative values.
Usually used to force a specified number of digits
to display.
Simple form:
.ToString(“D”)
.ToString(“D3”)
Example: 25
txtQuantity.text = intQuantity.ToString(”D”)
txtQuantity.text = intQuantity.ToString(”D3”)
025
Format Percent Function
To display numeric values as a percent
This function multiplies the argument by 100, adds a
percent sign and rounds to two decimal places.
Simple form:
.ToString(“P”)
.ToString(“P0”)
.ToString(“P3”)
Example: 50.50%
lblInterestRate.text = decInterestRate.ToString(“P”)
lblInterestRate.text = decInterestRate.ToString(“P0”)
50%
Format Date Time Function
Format an expression as a date and/or time
The expression may be a string that holds
A date or time values
A date type variable, or
A function that returns a date
Simple form:
ToLongDateString
ToShortDateString
ToLongTimeString
ToShortTimeString
Named Format
Named Format Returns Example
ToLongDateString Days of week, Month, Day, Sunday, June 21,2020
Year
ToShortDateString MM/DD/YY 6/21/2020
ToLongTimeString HH:MM:SS AM/PM 01:02:26 PM
ToShortTimeString HH:MM (24 hours clock) 12:03 AM
Private Sub btnCost_Click(sender As Object, e As EventArgs) Handles btnCost.Click
strNumberOfDays = txtNumberOfDays.Text
intNumberOfDays = Convert.ToInt32(strNumberOfDays)
lblTotalCost.Text = decTotalCost.ToString("C")
End Sub
Identifiers=Integer .Parse(Object.Property)
End of Topic 3