⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vel14.htm

📁 简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<PRE><FONT COLOR="#000080">Format$(Expression, FormatStr)</FONT></PRE><P>Often, you'll assign the result of the Format$() function to other variables and controls. Generally, you'll perform all needed calculations on numeric values before formatting those values. After you've performed the final calculations, you'll then format the values to string (or variant) data types and display the resulting answers as needed.<BR><P>The <I>Expression</I> can be a variable, expression, or constant. The <I>FormatStr</I> must be a value from Table 14.1.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Visual Basic contains many format strings in addition to the ones shown in Table 14.1. You can even develop your own <I>programmer-defined format strings</I>, although this book doesn't go into those.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>thousands separator</I> is a decimal point or comma inside numbers over 999.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 14.1. The fixed </B><FONT COLOR="#FF8000"><B><I>FormatStr</I></B></FONT><B> values.</B></FONT></CENTER><BR><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>FormatStr</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>&quot;Currency&quot;</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Ensures that a dollar sign, $, appears before the formatted value followed by a thousands separator (the country setting determines whether the thousands separator is a comma or a decimal), and that two decimal places show. Visual Basic displays negative values in parentheses.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>&quot;Fixed&quot;</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays at least one digit before and two digits following the decimal point with no thousands separator.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>&quot;General Number&quot;</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays the number with no thousands separator.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>&quot;Medium Time&quot;</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays the time in 12-hour format and the AM or PM indicator.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>&quot;On/Off&quot;</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays On if the value contains a nonzero or True value and displays Off if the value contains zero or a False value. These values correspond to a special internal representation of computer values called <I>binary numbers</I>.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>&quot;Percent&quot;</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays the number, multiplied by 100, and adds the percent sign to the right of the number.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>&quot;Scientific&quot;</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays numbers in scientific notation.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>&quot;Short Time&quot;</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays the time in 24-hour format.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>&quot;True/False&quot;</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays True if the value contains a nonzero or True value, and displays False if the value contains zero or a False value.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>&quot;Yes/No&quot;</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Displays Yes if the value contains a nonzero or True value, and displays No if the value contains zero or a False value.</FONT></TABLE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>You'll Rarely Need Format Codes: If the predefined formats from Table 14.1 don't match the format you need, you can define your own using special formatting codes. This unit would be at least twice as long as it is if all the programmer-defined formats were shown here. The good news is that, when you do define your own formats, you'll almost always use just a combination of the pound sign and zeros to format the values you need.<BR>Each pound sign in the format indicates where a digit goes, and the zero indicates that you want either leading or trailing zeros. The following assignment displays the value of Weight to three decimal places:<BR>lblMeas.Caption = Format$(Weight, &quot;######.000&quot;)<BR>You could also request that <I>no</I> decimal point should appear by formatting a fractional value such as Weight, and Visual Basic will round the number as needed to fit the target format. The following assignment displays Weight with no decimal places shown on the screen:<BR>lblMeas.Caption = Format$(Weight, &quot;######&quot;)</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 14.5 contains a series of formatting function calls that convert numeric and logical values to formatted variant data types that you can display.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>There are two formatting functions. Format$() returns a string and Format() returns a variant value. The Format$() function formats values so that the values look the way you want them to look. The predefined format strings make it easy to display numbers and logical values in the format that you require.<BR><P><FONT COLOR="#000080"><B>Listing 14.5. Formatting numeric and logical values.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Dim FormValue As String2:3: ' Change 12345.678 to $12,345.684: FormValue1 = Format$(12345.678, &quot;Currency&quot;)5:6: ' Change 12345678 to 12345.687: FormValue2 = Format$(12345.678, &quot;Fixes&quot;)8:9: ' Change .52 to 52.00%10: FormValue3 = Format$(.52, &quot;Percent&quot;)11:12: ' Change 1 to Yes13: FormValue4 = Format$(1, &quot;Yes/No&quot;)14:15: ' Change 0 to No16: FormValue5 = Format$(0, &quot;Yes/No&quot;)17:18: ' Change 1 to True19: FormValue6 = Format$(1, &quot;True/False&quot;)20:21: ' Change 0 to False22: FormValue7 = Format$(0, &quot;True/False&quot;)</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 4 converts the decimal single-precision number to a formatted currency string. If you were to display FormValue1 in a label's caption, the caption would show $12,345.68. Line 7 formats the same number for a noncurrency value with two decimal places. Line 10 changes a fractional number to its corresponding decimal percentage value and adds the percent sign after the formatted percent to show the percentage format. Line 13 changes the nonzero value of 1 to Yes. You can format any nonzero or relational result to a Yes, No, True, or False value, as shown in the program's remaining lines, so the values appear exactly the way you want them to look.<BR><BR><A NAME="E68E110"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E94"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>True or false: Now() returns information for <I>both</I> the current date and time.<BR><BR><LI>Where do the time and date functions get their values?<BR><BR><LI>What is the difference between 12-hour time and 24-hour time?<BR><BR><LI>True or false: 9:23 can be either 12-hour or 24-hour time.<BR><BR><LI>True or false: 9:23 a.m. can be either 12-hour or 24-hour time.<BR><BR><LI>What is the 24-hour time value for 7:54 a.m.?<BR><BR><LI>What is the difference between the Date$() and Date() functions?<BR><BR><LI>True or false: Visual Basic supports both a Now() and a Now$() function.<BR><BR><LI>Does Now() return its time value using a 12-hour or 24-hour clock?<BR><BR><LI>True or false: The time and date functions ignore your international time and date settings.<BR><BR><LI>True or false: Date() and Time() set your computer's date and time.<BR><BR><LI>Which function, Date() or Date$(), should you use if you think that your computer program will still be in use in the year 2000?<BR><BR><LI>Describe the purpose of the Timer() function.<BR><BR><LI>What is a <I>byte</I>?<BR><BR><LI>What data type does a serial date value require?<BR><BR><LI>How does the TimeValue() function relate to the Hour(), Minute(), and Second() functions?<BR><BR><LI>What is a <I>logical value</I>?<BR><BR><LI>What functions format numeric output for you?<BR><BR><LI>What is the difference between Format() and Format$()?<BR><BR><LI>What is a <I>thousands separator</I>?<BR><BR><LI>What is the difference between the &quot;Fixed&quot; and &quot;Currency&quot; fixed-format strings?<BR><BR></OL><BR><A NAME="E69E95"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Find the Bug</B></FONT></CENTER></H4><BR><OL><LI>Consider the user's input shown in Figure 14.3. The program is setting a time with this input box request:<BR><BR><LI>  Time$ = InputBox(&quot;What do you want to set the time to?&quot;)<BR><BR><LI>  Why do you suppose that Visual Basic indicates (as soon as the user presses OK in the input box) that an error occurred?<BR><BR></OL><P><B> <A HREF="14vel03.gif">Figure 14.3. Something's going to be wrong here.</A></B><BR><BR><A NAME="E69E96"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><P>Write code that asks the user for the time that she or he clocked into work and then asks for the time that she or he clocked out. Display in three labels the total amount of seconds worked, the total number of minutes worked, and the total number of hours worked.<BR><P ALIGN=LEFT><A HREF="vel13.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="velp07.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -