CIS 451 E-Commerce Application Development
Download
Report
Transcript CIS 451 E-Commerce Application Development
CIS 338: Operators and
Formatting in VB.NET
Dr. Ralph D. Westfall
April, 2011
Things Can Do in Statements
nAge = 57
sValue = CStr(1234.56)
dPrice = ListPrice(dCost)
[Call] PrintData(sStates())
frmInput.Text = "Inputs"
If nAge > 17 Then …
(assign value)
(VB function)
(user function)
(procedure)
(set property)
(test value[s])
Assignment Statements
nCount = 0
sState(1) = "AK"
dTotal = dTotal + dItemCost
or dTotal += dItemCost
'above is extremely common code pattern
sFullName = "Abraham " & "Lincoln"
'concatenation
sMonth = CStr(Month(Now)) ' function
Math: Operator Precedence
1.
2.
3.
4.
5.
6.
7.
highest to lowest level (PEMDAS)
( )
parentheses
^
exponentiation (raise to a power)
–
negation (reverse the sign)
*, /
multiply, divide
\
integer division (no decimals)
mod modulus arithmetic (remainder)
+, –
add, subtract
calculate left to right if on same level
Integer Division and Modulus
\ (backslash) gives integer part of result
of dividing 2 integers e.g., 7\4 = 1
doesn't work with decimal numbers if
Option Strict On, decimal numbers are
rounded to integers if Option Strict Off
Mod gives remainder after integer
division e.g., 7 Mod 4 = 3
ASCII Codes (decimal values)
0 – 31 = printer control commands
32 – 47, 58 – 64, 91 – 96 are all
punctuation marks
32=space, 33=!, 34=", 35=#, 36=$, etc.
48 – 57 = 0 – 9
65 – 90 = A – Z
97 – 122 = a – z
Wikipedia article (reasons for choices)
Working with ASCII Codes
sChar = Chr(65) [sChar value is "A"]
returns letter of code number in
parentheses
nChar = Asc("a") [nChar value is 97]
returns ASCII code number of letter in
parentheses
vbCrLf = carriage return & line feed
ASCII characters 13 and 10 together
shows text on separate lines e.g., MsgBox
Formatting Outputs
general formatting function
[string].Format(argument,[format])
defaults: Start>Control Panel> Regional
and Language (try on own computer)
argument is value or variable to format
use format style name, or format string
TextBox1.Text = _'line continuation
Format(CStr(1.0/7.0), "Fixed")
TextBox2.Text=Format(3/0.7,"#.000")
Named Number Format Styles
General Number – as is
e.g., 1234.56789
Currency – symbol, commas, 2 decimals
e.g., $1,234.57 (rounds: 2 decimals)
Fixed – 1+ digit left of decimal, 2 on
right
e.g., 0.91, 1234.57 (rounds: 2 places)
Standard – like fixed, but with commas
e.g., 1,234.57 (also rounds)
'notes
Number Format Styles - 2
Percent – 0.123 shown as 12.3%
Scientific - for very big or very small #s
6.78E12 = 6,780,000,000,000 ' *10^12
6.78E-12 = 0.00000000000678 ' *10^-12
Yes/No: No = 0, all other #s = Yes
True/False: like Yes/No, 0 = False
On/Off: like Yes/No, 0 = Off
Format Strings for Numbers
0 – shows the digit, or 0 if it is zero
# – shows digits > 0, and spaces for leading
and trailing zeros (e.g., 0010.200 10.2)
. (decimal) , (comma)
'insert punctuation
% – shows %, multiplies decimals by 100
format strings enclosed in quotes
Format(fCost, "0.00")
other examples: "#,###.####"
"#.0%"
Format Strings for Numbers - 2
can use 1-3 argument strings to format
numeric data (separated by semicolons)
1 string: for all values
' "$#,##0"
2 strings: 1st is for positives & zeros, 2nd is
for negatives
' "$#,##0;($#,##0)"
3 strings: 1st is for positives, 2nd negatives,
3rd zeros
' "$#,##0;($#,##0);--"
skip string: uses previous ' "$#,##0;;--"
Named Date/Time Format
Styles
enclose date data with pound signs
#yyyy-mm-dd hh:mm:ss#
Format(#3/15/2010#, "General Date")
General Date – date, time
10/10/01 11:46:04 PM 'VS changes to 2001
Long Date or Medium Date –
Wednesday, October 13, 2010
Short Date – 10/11/2010
Named Time Format Styles
Format(#6:30:00 AM#, "Long Time")
Long Time or Medium Time – 11:53:38 PM
Short Time – 11:53 PM
Military Time? used to be available, but I
couldn't find a predefined format for this
Format Strings for Dates
Format(datNow, "mm/dd/yy")
month: m 7 mm 07 mmm Jul
day:
d 3 dd 03 ddd
Wed
year: yy 05 yyyy 2005
/ slash
separator
other strings for dates, and also for
time formatting
FormatCurrency Function
defaults from Control Panel Regional
and Language Options
numeric formats
FormatCurrency(Expression 'numeric
[,NumDigitsAfterDecimal
[,IncludeLeadingDigit *
[,UseParensForNegativeNumbers *
[,GroupDigits *]]]])
'*TriState.True [False, UseDefault]
Formatting Functions
defaults [English (United States)]
FormatCurrency
FormatNumber
$12,345.67
–12,345.67
' –
for negative #s
FormatPercent
1234567% 'times 100
FormatDateTime mm/dd/yyyy hh:mm:ss
Round
12346
'default = whole # if don't specify decimals
Working with Dates
Now – gives current time/date
Month(Now), Day(Now), or Year(Now)
gives current month, day or year number
Format(Now, "w") day of week (Sun = 1)
1st day of week is different in some countries
e.g., Monday in France
Dim datShop as Date = DateDiff("d", Now,
CDate("12/25/2011")))
datAddHalfMon = DateAdd("w", 2, Now)
w = # of weekdays, ww= # of weeks,
m=# of months, y=# of years in formula