CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting...

10
CSC 162 Visual Basic I Programming

Transcript of CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting...

Page 1: CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.

CSC 162

Visual Basic I Programming

Page 2: CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.

Randomizing and Formatting• Randomizing

• Formatting– User-Defined Formats– Named Numeric Formats

Page 3: CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.

Randomizing• When using the random number generator (Rnd),

the same sequence of “random” numbers will be produced for each execution of the program.

• This is due to the fact that there is a human-written algorithm for producing the “random” numbers.

• To generate a different sequence, the random number generator must be seeded.

• A seed tells the random number generator where to start in the sequence of “random” numbers.

Page 4: CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.

Randomizing• The most common seed for the random number

generator is using the time (clock).

• Visual Basic has a built-in sub procedure (called Randomize) for seeding Rnd with the clock.

• At the beginning of any procedure that uses Rnd, include this call:Call Randomize

Page 5: CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.

Formatting• Visual Basic has a built-in function for formatting

numeric output:Format(x, y)

wherex is the number being formatted, and

y is a string showing the format that the

number should take (uses

formatting flags)

• Format automatically rounds the number.

Page 6: CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.

User-Defined Formats• Formatting flags

– 0 indicates a required digit (if no digit, shows ‘0’)– # indicates a non-zero digit placeholder (if zero digit, shows

nothing)– , indicates a thousands separator– . indicates a decimal separator– $ indicates a dollar sign– % indicates a percent sign (and multiplies number by 100)– E+ indicates scientific notation exponent with uppercase E and

both positive and negative exponents are displayed– e+ indicates scientific notation exponent with lowercase e and

both positive and negative exponents are displayed– E- indicates scientific notation exponent with uppercase E and

only negative exponents are displayed– e- indicates scientific notation exponent with lowercase e and

only negative exponents are displayed

Page 7: CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.

User-Defined Formats• Example:

sngX = 1232.8379Print Format(sngX, "0") ' 1233Print Format(sngX, "0.000") ' 1232.838Print Format(sngX, "0.00000") ' 1232.83790Print Format(sngX, "##,###") ' 1,233Print Format(sngX, "00,000") ' 01,233Print Format(sngX, "##,##0.0") ' 1,232.8Print Format(sngX, "$0.00") ' $1232.84Print Format(sngX, "0.0E+00") ' 1.2E+03Print Format(sngX, "0.0e+00") ' 1.2e+03Print Format(sngX, "0.0e-00") ' 1.2e03

• Example:sngY = 0.05783Print Format(sngY, "0.0E+00") ' 5.78E-02Print Format(sngY, "0.0e-00") ' 5.78e-02Print Format(sngY, "0.0%") ' 5.8%

Page 8: CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.

Named Numeric Formats• Instead of creating strings of formatting flags, Visual

Basic has several pre-defined formats.Named Numeric Format Result"General Number" Number with no thousands separator"Fixed" Number with no thousands separator, at least one

digit left of the decimal, and two digits right of the decimal

"Standard" Number with thousands separator, at least one digit left of the decimal, and two digits right of the decimal

"Currency" Number with currency symbol, thousands separator, at least one digit left of the decimal,

and two digits right of the decimal"Scientific" Number in scientific notation with one digit left of

the decimal, two digits right of the decimal, uppercase E, and always shows sign of exponent

Page 9: CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.

Named Numeric FormatsNamed Numeric Format Result"Percent" Multiplies the number by 100 and displays it with

with one digit left of the decimal, two digits right of the decimal, and the percent

symbol

• Example:sngZ = 2.579Print Format(sngZ, "Currency") ' $2.58Print Format(sngZ, "Percent") ' 257.90%

• Example:sngA = 45.873Print "The cost is " & Format(sngA, "Currency") & "."

Output:The cost is $45.87.

Page 10: CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.

Lab AssignmentUsing text boxes, ask the user for a price and a sales tax

rate (as a percent). Compute the total selling price and display the result (in a label) as follows:

An item priced $5.75 at a tax rate of 7.25% costs $6.17.

Programming Assignment 5Due Monday, November 10 / Tuesday, November 11Page 241 #6.18Page 241 #6.19