COS120 LECTURE 5 - 1 COS120 Chapter 3.5 Input and Output...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 COS120 Chapter 3.5 Input and Output Format Functions: When displaying a number to an output object, it is often formatted to look more attractive. Examples include restricting the number of digits, or adding a $ or % symbol. VB offers a number of formatting functions to assist in output. The FormatNumber function returns a string of the value rounded to the specified number of decimal places Dim value As Double = 23.27845 With lstOut.Items .Add (FormatNumber (value, 0)) .Add (FormatNumber (value, 1)) .Add (FormatNumber (value, 2)) .Add (FormatNumber (value, 3)) End With Output: 23 23.3 23.28 23.278 Note: The values returned by the FormatNumber function are strings. Do not assign their values to numeric variables. This function is usually only used in an output statement to a label or list box. The FormatCurrency returns a string of the value rounded to the specified number of decimal places with a dollar sign. The default number of decimal places is 2 so the second argument may be omitted. Dim value As Double = 356.134 With lstOut.Items .Add (FormatCurrency (value, 0)) .Add (FormatCurrency (value, 2)) .Add (FormatCurrency (value)) End With Output: $356 $356.13 $356.13 2 The FormatPercent function returns a string of the value rounded to the specified number of decimal places in percentage format. The value is multiplied by 100 and the % sign is added. The default number of decimal places is 2 so the second argument may be omitted. Dim value As Double = 0.37801 With lstOut.Items .Add (FormatPercent (value, 0)) .Add (FormatPercent (value, 1)) .Add (FormatPercent (value)) End With Output: 38% 37.8% 37.80% Format Zones: Data can be displayed in a tabular form in a list box. In order to line up columns you should use a monospaced font , such as Courier . In a monospaced font, each character has exactly the same width. A format string is created with directions to VB on how the columns are to be created. Each column (zone) has a number (zone 0, zone 1, etc.) and a width. A format string is a group of zone, width pairs enclosed in curly brackets. Data is normally right aligned in its zone. If the width values are negative, data is left aligned in its zone. Spaces between curly bracket pairs are interpreted as spaces between zones. Dim fmtStr As String = {0,-15}{1,-10}{2,-8} |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ | Zone 0 Zone 1 Zone 2 15 characters 10 characters 8 characters A format string is used in a call to the String.Format function. This function is passed the format string followed by the data items to place in the respective zones....
View Full Document

Page1 / 7

COS120 LECTURE 5 - 1 COS120 Chapter 3.5 Input and Output...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online