定義數字數據
數字數據項用PICTURE子句定義。數據描述中的每個9代表一個十進制位。
可以出現的特殊含義字元有: P指示前導0和尾部0 S指示符號,正號或負號 V隱含的小數點
數據是關於某些方面的一組數字。
顯示數字數據
為了讓數字數據在顯示和列印的時候更容易閱讀,可以使用編輯標誌(如:小數點,逗號,美元符號和借方符號貸方符號)格式化數字數據項。
例如,在下面的代碼中Edited-price就是編輯的數字數據項:
05 Price Pic 9(5)v99.
05 Edited-price Pic $zz,zz9.99.
. . .
Move Price To Edited-price
Display Edited-price
Move Edited-price to Price
Display Price
控制數字數據的存儲
COBOL程式中的數字數據可以以下格式存儲: 外部十進制(USAGE DISPLAY) 外部浮點型(USAGE DISPLAY) 內部十進制(USAGE PACKED-DECIMAL) 二進制(USAGE BINARY) 本地二進制(USAGE COMP-5) 內部浮點型(USAGE COMP-1, USAGE COMP-2)
COMP和COMP-4等價於BINARY(二進制),COMP-3等價於(打包十進制)PACKED-DECIMALCOMP。
算術運算
OBNOL算術運算有以下幾種方式: COMPUTE, ADD, SUBTRACT, MULTIPLY, 和 DIVIDE 語句 數字內置函式 語言環境中可調用的服務
4.4.1 算術語句 COMPTE語句把表達式的值賦給一個或多個數據項。用COMPUTE語句可以輕鬆的把算術操作組合在一起,而不受ADD,SUBSTRACT, MULTIPLY, 和 DIVIDE語句那樣的結果存放限制。
4.4.1.1 ADD語句 ADD 語法1:
ADD identifier-1,identifier-2,… TO identifier-n [ROUNDED]
[ON SIZE ERROR statement-1]
[NOT ON SIZE ERROR statement-2]
[END-ADD]
例子:
ADD NUM1 TO NUM2.
ADD NUM1, 10 TO NUM2.
ADD NUM1, NUM2 TO NUM3 ON SIZE ERROR PERFORM 100-ERROR.
ADD 語法2:
ADD identifier-1,identifier-2,… [TO] identifier-n GIVING identifier-m [ROUNDED]
[ON SIZE ERROR statement-1]
[NOT ON SIZE ERROR statement-2]
[END-ADD]
例子:
ADD NUM1 TO NUM2 GIVING NUM3.
ADD NUM1, NUM2 GIVING NUM3.
ADD NUM1, 10 TO NUM2 GIVING NUM3.
ADD 語法3:
ADD CORR identifier-1 TO identifier-2 [ROUNDED]
[ON SIZE ERROR statement-1
[NOT ON SIZE ERROR statement-2] [END-ADD]
例子:
03 CURRENT-MONTH.
05 PAY PIC 9(3)V9(2).
05 TAX PIC 9(2)V9(2).
03 YTD.
05 PAY PIC 9(3)V9(2).
05 TAX PIC 9(2)V9(2).
ADD CORR CURRENT-MONTH TO YTD.
4.4.1.2 SUBTRACT語句 SUBTRACT 語法1:
SUBTRACT identifier-1, identifier-2,… FROM identifier-n
[ROUNDED]
[ON SIZE ERROR statement-1
[NOT ON SIZE ERROR statement-2]
[END-SUBTRACT]
例子:
SUBTRACT NUM1 FROM NUM2.
SUBTRACT 語法2:
SUBTRACT identifier-1 identifier-2,… FROM identifier-n
GIVING identifier-m [ROUNDED]
[ON SIZE ERROR statement-1]
[NOT ON SIZE ERROR statement-2]
[END-SUBTRACT]
例子:
SUBTRACT NUM1 FROM NUM2 GIVING NUM3.
SUBTRACT 語法3:
SUBTRACT CORR identifier- 1 FROM identifier- 2
[ROUNDED]
[ON SIZE ERROR statement-1]
[NOT ON SIZE ERROR statement-2]
[END-SUBTRACT]
例子:
SUBTRACT CORR REC1 FROM REC2.
4.4.1.3 MULTIPLY語句 MULTIPLY 語法1:
MULTIPLY identifier- 1 BY identifier-2
[ROUNDED]
[ON SIZE ERROR statement- 1]
[NOT ON SIZE ERROR statement-2]
[END-MULTIPLY]
例子:
MULTIPLY NUM1 BY NUM2.
MULTIPLY 語法2:
MULTIPLY identifier- 1 BY identifier- 2
GIVING identifier- 3
[ROUNDED]
[ON SIZE ERROR statement-1]
[NOT ON SIZE ERROR statement-2]
[END-MULTIPLY]
例子:
MULTIPLY NUM1 BY NUM2 GIVING NUM3.
4.4.1.4 DIVIDE語句 DIVIDE 語法1:
DIVIDE identifier- 1 INTO identifier-2
[ROUNDED]
[ON SIZE ERROR statement-1]
[NOT ON SIZE ERROR statement-2]
[END-DIVIDE]
例子:
DIVIDE NUM1 INTO NUM2.
DIVIDE 語法2:
DIVIDE identifier-1 INTO identifier-2
GIVING identifier-3
[ROUNDED]
[ON SIZE ERROR statement- 1]
[NOT ON SIZE ERROR statement- 2]
[END-DIVIDE]
例子:
DIVIDE NUM1 INTO NUM2 GIVING NUM3.
DIVIDE 語法3:
DIVIDE identifier- 1 BY identifier- 2
GIVING identifier- 3
[ROUNDED]
[ON SIZE ERROR statement- 1]
[NOT ON SIZE ERROR statement-2]
[END-DIVIDE]
例子:
DIVIDE NUM1 BY NUM2 GIVING NUM3.
DIVIDE 語法4:
DIVIDE identifier- 1 INTO identifier-2
GIVING identifier-3
[ROUNDED]
REMAINDER identifier-4
[ON SIZE ERROR statement-1]
[NOT ON SIZE ERROR statement-2]
[END-DIVIDE]
例子:
DIVIDE NUM1 INTO NUM2 GIVING NUM3 REMAINDER NUM4.
DIVIDE 語法5:
DIVIDE identifier-1 BY identifier-2
GIVING identifier-3
[ROUNDED]
REMAINDER identifier-4
[ON SIZE ERROR statement-1]
[NOT ON SIZE ERROR statement-2]
[END-DIVIDE]
例子:
DIVIDE NUM1 BY NUM2 GIVING NUM3 REMAINDER NUM4.
4.4.1.5 COMPUTE語句 COMPUTE 語法:
COMPUTE identifier-1 [ROUNDED], identifier-2 [ROUNDED],…
= arithmetic-expression
[ON SIZE ERROR statement-1]
[NOT ON SIZE ERROR statement-2]
[END-COMPUTE]
例子:
COMPUTE RESULT=NUM1*NUM2+NUM3-NUM4.
4.4.2 算術表達式 允許數字數據項出現的地方都可以替換為算術表達式。
算術表達式可以有正負號(+,-),冪指數符號(**),除號或乘號(/,*)和加號或減號(+,-)。
4.4.3 數字內置函式 數字內置函式只能出現在數字表達式允許出現的地方。
數字內置函式返回一個有符號的數值。函式本身可以看成臨時數字數據項。
下面是一些數字內置函式: 數字處理: LENGTH, MAX, MIN, NUMVAL, NUMVAL-C, ORD-MAX, ORD-MIN 日期和時間: CURRENT-DATE, DATE-TO-YYYYMMDD, …etc 數學: INTEGER, LOG, SUM, SIN, COS, MOD, …etc.
例子:
COMPUTE X = LENGTH (RECORD).
COMPUTE Y = MAX (X, Y).