Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats."— Presentation transcript:

1 CSC 162 Visual Basic I Programming

2 Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats

3 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.

4 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

5 Formatting Visual Basic has a built-in function for formatting numeric output: Format(x, y) where x 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.

6 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

7 User-Defined Formats Example: sngX = 1232.8379 Print Format(sngX, "0")' 1233 Print Format(sngX, "0.000")' 1232.838 Print Format(sngX, "0.00000")' 1232.83790 Print Format(sngX, "##,###")' 1,233 Print Format(sngX, "00,000")' 01,233 Print Format(sngX, "##,##0.0")' 1,232.8 Print Format(sngX, "$0.00")' $1232.84 Print Format(sngX, "0.0E+00")' 1.2E+03 Print Format(sngX, "0.0e+00")' 1.2e+03 Print Format(sngX, "0.0e-00")' 1.2e03 Example: sngY = 0.05783 Print Format(sngY, "0.0E+00")' 5.78E-02 Print Format(sngY, "0.0e-00")' 5.78e-02 Print Format(sngY, "0.0 % ")' 5.8 %

8 Named Numeric Formats Instead of creating strings of formatting flags, Visual Basic has several pre-defined formats. Named Numeric FormatResult "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

9 Named Numeric Formats Named Numeric FormatResult "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.579 Print Format(sngZ, "Currency")' $2.58 Print Format(sngZ, "Percent")' 257.90 % Example: sngA = 45.873 Print "The cost is " & Format(sngA, "Currency") & "." Output: The cost is $45.87.

10 Lab Assignment Using 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 5 Due Monday, November 10 / Tuesday, November 11 Page 241 #6.18 Page 241 #6.19


Download ppt "CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats."

Similar presentations


Ads by Google