Q(uick)BASIC Function: CVD
Quick View
CVD
A function that converts strings containing numeric values to numbers
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- MKI$(integer-expression%)
- MKL$(long-integer-expression&)
- MKS$(single-precision-expression!)
- MKD$(double-precision-expression#)
- CVI(2-byte-numeric-string)
- CVL(4-byte-numeric-string)
- CVS(4-byte-numeric-string)
- CVD(8-byte-numeric-string)
Description/Parameter(s)
MKI$, MKL$, MKS$, and MKD$ convert numbers to numeric strings that can be stored in FIELD statement string variables. CVI, CVL, CVS, and CVD convert those strings back to numbers.
Function | Returns | Function | Returns |
MKI$ | A 2-byte string | CVI | An integer |
MKL$ | A 4-byte string | CVL | A long integer |
MKS$ | A 4-byte string | CVS | A single-precision number |
MKD$ | An 8-byte string | CVD | A double-precision number |
Syntax
- CVI(2-byte-string)
- CVL(4-byte-string)
- CVS(4-byte-string)
- CVD(8-byte-string)
Description/Parameter(s)
CVI, CVS, CVL, and CVD are used with a FIELD statement to read real numbers from a random-access file. The functions take strings defined in the FIELD statement and convert them to a value of the corresponding numeric type. The functions are the inverse of MKI$, MKS$, MKL$, and MKD$:
Function | Description |
CVI | Converts a 2-byte-string created with MKI$ back to an an integer. |
CVS | Converts a 4-byte-string created with MKS$ back to a single-precision number. |
CVL | Converts a 4-byte-string created with MKL$ back to a long integer. |
CVD | Converts an 8-byte-string created with MKD$ back to a double-precision number. |
Note: | The new BASIC record variables provide a more efficient and convenient way of reading and writing random-access files. |
Example
The following program illustrates the use of MKS$ and CVS. If you run this program, you will be prompted to enter an "account #." The account numbers in the sample data file ACCOUNT.INF are 1, 2, 3, 4, and 5.
After you enter an account number, the program gives you a chance to update the account balance.
' *** Example program that uses the CVS function ***
'
' Define a user type for the data records.
TYPE Buffer
AccName AS STRING * 25
Check AS STRING * 4
END TYPE
' Define a variable of the variable type
DIM BankBuffer AS Buffer
OPEN "ACCOUNT.INF" FOR RANDOM AS #1 LEN = 29
CLS
RESTORE
READ AccName$, Check
I = 0
DO WHILE UCASE$(AccName$) <> "END"
I = I + 1
BankBuffer.AccName = AccName$
BankBuffer.Check = MKS$(Check)
PUT #1, I, BankBuffer
READ AccName$, Check$
IF AccName$ = "END" THEN EXIT DO
LOOP
CLOSE #1
'
DATA "Bob Hartzell", 300
DATA "Alice Provan", 150
DATA "Alex Landow", 75
DATA "Walt Riley", 50
DATA "Georgette Gump", 25
DATA "END", 0
See also:
Syntax
- CVI(2-byte-string)
- CVL(4-byte-string)
- CVS(4-byte-string)
- CVD(8-byte-string)
- CVC(8-byte-string)
Description/Parameter(s)
CVI, CVL, CVS, CVD, and CVC convert strings containing numeric values to numbers. The strings are created by MKI$, MKL$, MKS$, MKD$, and MKC$.
- CVI converts a 2-byte string created with MKI$ back to an integer.
- CVL converts a 4-byte string created with MKL$ back to a long integer.
- CVS converts a 4-byte string created with MKS$ back to a single-precision number.
- CVD converts an 8-byte string created with MKD$ back to a double-precision number.
- CVC converts an 8-byte string created with MKC$ back to a currency number.
Usage Notes
- CVI, CVL, CVS, CVD, and CVC are used with a FIELD statement to read numbers from a random-access file. The functions take strings defined in the FIELD statement and convert them to a value of the corresponding numeric type.
- The functions are the inverse of MKI$, MKL$, MKS$, MKD$, and MKC$.
- These BASIC record variables provide a more efficient and convenient way of reading and writing random-access files than do some older versions of BASIC.
Example
This example uses the MKS$ and CVS functions to convert values to and from strings for storage in a random-access file.
If you run this program, you will be prompted to enter an account number. The account numbers in the sample data file ACCOUNT.INF are 1 through 5.
'Define a user type for the data records.
TYPE Buffer
AccName AS STRING * 25
Check AS STRING * 4
END TYPE
'Define a variable of the variable type.
DIM BankBuffer AS Buffer
OPEN "ACCOUNT.INF" FOR RANDOM AS #1 LEN = 29
CLS
RESTORE
READ AccName$, Check
I% = 0
DO WHILE UCASE$(AccName$) <> "END"
I% = I% + 1
BankBuffer.AccName = AccName$
BankBuffer.Check = MKS$(Check)
PUT #1, I%, BankBuffer
READ AccName$, Check$
IF AccName$ = "END" THEN EXIT DO
LOOP
CLOSE #1
DATA "Bob Hartzell", 300
DATA "Alice Provan", 150
DATA "Alex Landow", 75
DATA "Walt Riley", 50
DATA "Georgette Gump", 25
DATA "END", 0
OPEN "ACCOUNT.INF" FOR RANDOM AS #2 LEN = 29
FIELD #2, 25 AS AccName$, 4 AS Check$
DollarFormat$ = "$$#####.##"
DO
PRINT
DO
CLS
INPUT "Enter account # to update: ", Rec%
GET #2, Rec% 'Get the record
PRINT : PRINT "This is the account for "; AccName$
PRINT : INPUT "Is this the account you wanted"; R$
LOOP WHILE UCASE$(MID$(R$, 1, 1)) <> "Y"
'Convert string to single-precision value.
Checkamt! = CVS(Check$)
PRINT
PRINT "The opening balance for this account is";
PRINT USING DollarFormat$; Checkamt!
PRINT : PRINT "Enter the checks and cash withdrawals for this"
PRINT "account below. Enter 0 when finished."
PRINT
DO
INPUT "Enter amount -> ", Checkout!
Checkamt! = Checkamt! - Checkout!
LOOP UNTIL Checkout! = 0
PRINT
PRINT "Enter the deposits for this account below."
PRINT "Enter 0 when finished."
PRINT
DO
INPUT "Enter amount ->", Checkin!: Checkamt! = Checkamt! + Checkin!
LOOP UNTIL Checkin! = 0
PRINT
PRINT "The closing balance for this account is";
PRINT USING DollarFormat$; Checkamt!
'Convert single-precision number to string.
LSET Check$ = MKS$(Checkamt!)
PUT #2, Rec% 'Store the record.
PRINT : INPUT "Update another"; R$
LOOP UNTIL UCASE$(MID$(R$, 1, 1)) <> "Y"
CLOSE #2
KILL "ACCOUNT.INF"
END