COS120-Ch3.5InputOutput - COS120 Chapter 3.3 Input and...

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

View Full Document Right Arrow Icon
COS120 Chapter 3.3 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 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
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}”
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/06/2008 for the course COS 120 taught by Professor Carolroberts during the Spring '08 term at University of Maine Orono .

Page1 / 7

COS120-Ch3.5InputOutput - COS120 Chapter 3.3 Input and...

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