如何在.net中将数字格式化为S9(5)V99 ascii
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3257771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to format a number to S9(5)V99 ascii in .net
提问by user392973
I've been searching for s9(5)v99 but got different information and not really clear. Could someone shows how or the formula to convert. thanks
我一直在寻找 s9(5)v99 但得到了不同的信息并且不太清楚。有人可以展示如何或公式进行转换。谢谢
回答by NealB
What you have shown us here is
the PICTUREclause portion of a COBOL data declaration.
您在此处向我们展示PICTURE的是 COBOL 数据声明的子句部分。
COBOL data declarations are a bit odd and take some getting used to. Here is a link to an introductory tutorial on COBOL data declarations. This should get you started.
COBOL 数据声明有点奇怪,需要一些时间来适应。这是COBOL 数据声明的介绍性教程的链接。这应该让你开始。
The PICture clause you have given in your question is defining a numeric item with the following characteristics:
您在问题中给出的 PICture 子句定义了一个具有以下特征的数字项:
S- Leading sign9(5)- 5 decimal digitsV- Implied decimal point99- 2 digits after the implied decimal point
S- 领先标志9(5)- 5 位十进制数字V- 隐含小数点99- 隐含小数点后的 2 位数字
Basically, you are telling the COBOL compiler to define a numeric variable capable of holding
the values -99999.99 through +99999.99. Exactly how the compiler will fulfill this
request depends on the specific USAGEclause. However, for numeric items containing a
fixed decimal position, the 'normal' USAGE is PACKED-DECIMALor COMP-3(these are just
different names meaning the same thing). This linkprovides some introductory information concerning the storage representation of packed decimal data.
基本上,您是在告诉 COBOL 编译器定义一个能够保存值 -99999.99 到 +99999.99 的数字变量。编译器将如何满足此请求取决于特定USAGE子句。但是,对于包含固定小数位的数字项,“正常”用法是PACKED-DECIMAL或COMP-3(这些只是不同的名称,意思相同)。此链接提供了一些有关压缩十进制数据的存储表示的介绍性信息。
Packed decimal data are useful for doing numeric computations where the number of decimal points must remain fixed.
压缩十进制数据对于小数点数必须保持固定的数值计算非常有用。
Writing packed decimal data to a report or terminal does not work particularly well. You must
first convert it to a DISPLAYable format. This involves MOVEing the packed decimal value to another
variable with a USAGE DISPLAYattribute. Suppose your packed decimal variable was called
PACKED-DECIMAL-NBRand was holding the value -2345.01. You could define a display variable
to hold it as:
将压缩十进制数据写入报告或终端的效果不是特别好。您必须首先将其转换为可用的DISPLAY格式。这涉及MOVE将压缩十进制值转换为具有USAGE DISPLAY属性的另一个变量。假设您的压缩十进制变量被调用
PACKED-DECIMAL-NBR并保存值 -2345.01。您可以定义一个显示变量将其保存为:
01 DISPLAY-NBR PIC +++,++9.99.
then when it comes time to write/display the value contained in PACKED-DECIMAL-NBRyou would
do something like:
那么当需要写入/显示包含在PACKED-DECIMAL-NBR您中的值时,您会执行以下操作:
MOVE PACKED-DECIMAL-NBR TO DISPLAY-NBR
DISPLAY DISPLAY-NBR
The MOVEconverts the packed-decimal number to a character representation which you can
display in reports or on the terminal. The value -2,345.01is displayed.
该MOVE转换填充十进制数的字符表示,你可以在报告或在终端上显示。-2,345.01显示值。
回答by cobol
S in cobol is the definition of a signed numeric field (ie can be positive or negative). So for example S999 is a signed 3 digit numeric (binary). Problem is S is assumed, and in most cobol compilers, the way the sign is marked is by "overpunching" the last character with another character to signify plus or minus. This results, if you look at the field in an ascii browser, you will have a strange character. Ie. MINUS 500 would look something like this 50C or 50} depending on the underlying character and if it were a + or - that had overpunched.
cobol 中的 S 是有符号数字字段的定义(即可以是正数或负数)。因此,例如 S999 是有符号的 3 位数字(二进制)。问题是假设 S ,并且在大多数 cobol 编译器中,标记符号的方式是用另一个字符“过冲”最后一个字符来表示加号或减号。结果,如果您在 ASCII 浏览器中查看该字段,您将看到一个奇怪的字符。IE。MINUS 500 看起来像这样的 50C 或 50},具体取决于底层字符,以及它是否是一个 + 或 - 已经过冲。
回答by Mikael Bergstr?m
Public Function CobolToDec(ByVal str As String) As Double
公共函数 CobolToDec(ByVal str As String) As Double
' **********************************************************************
' * Purpose: Converts Cobol numbers into dubble-values in MS format.
' * Author: Mikael Bergstrom
' * Date: 2017-11-06
' * Input: str = Cobolcoded value for amount S9(13)V99(Cobol)
' * Signs are in last byte
' **********************************************************************
Dim strNeg As String
strNeg = "pqrstuvwxy"
' If letter in strNeg then convert to number and negative sign
' p = 0, q = 1, r = 2 osv...
' Two last digits are decimals.
' Ex 00000000001200y = -120,09
' Ex 000000000015000 = 150,00
' Contains negative sign?
If InStr(1, str, "-", vbTextCompare) Then
dectal = CDbl(Trim(str))
CobolToDec = dectal
Else
' ...if not sign -> convert last digit to represetative digit and make hole number negative.
lastbyte = LCase(Right(str, 1))
If Not IsNumeric(lastbyte) Then
If InStr(1, strNeg, lastbyte, vbTextCompare) > 0 Then
dectal = "-" & Left(str, 13) & "," & Mid(str, 14, 1) & CStr(InStr(strNeg, lastbyte) - 1)
Else
' not valid negative sign in strNeg (pqrstuvwxy)
' Set 0 ast last sign as we don?t know and make negative number
dectal = "-" & Left(str, 13) & "," & Mid(str, 14, 1) & "0"
End If
Else
' positive number, just separate the two decimals
dectal = Left(str, 13) & "," & Right(str, 2)
End If
' Convert into double datatype and return result
CobolToDec = CDbl(FormatNumber(dectal, 2, , , vbFalse))
End If
End Function
结束函数
回答by John Saunders
PIC S9(5)v99isn't a number format. It's a description of how the data are stored and what it means there. For instance, a number stored as "-0010000" means -100.00.
PIC S9(5)v99不是数字格式。它描述了数据的存储方式及其含义。例如,存储为“-0010000”的数字表示 -100.00。
What exactly are you trying to accomplish? Are you trying to send a decimal-100.00 to a COBOL program?
你到底想完成什么?您是否decimal要向 COBOL 程序发送-100.00?
PIC -99,999.00isa number format. It specifies to use a leading minus sign if the number is negative, to use five digits before the decimal place, with a comma between the thousands, a decimal point, then exactly two digits after. A number stored in a PIC S9(5)V99field might reasonably be moved to a PIC -99,999.00field.
PIC -99,999.00是数字格式。它指定如果数字是负数,则使用前导减号,在小数位前使用五位数字,千位之间用逗号,小数点,然后是两位数。存储在PIC S9(5)V99字段中的数字可以合理地移动到PIC -99,999.00字段中。

