Altair Absent Member.
Absent Member.
2586 views

Error_StringToDecimalParse

Jump to solution

Hi,

When converting a string to decimal number the result displayed is wrong. Attached the source.
Anyone know how to resolve this problem?

      $set sourceformat"free"
 program-id. Program1 as "ConsoleApplication1.Program1".
 environment division.
 data division.
 working-storage section.
 01 ws-str1 string value "1234.12".
 01 ws-alpha1 pic x(20) value "1234.12".
 01 ws-number pic +9(5).99.
 01 ws-decimal decimal.
 procedure division.
***** Old School COBOL (ANSI85 Intrinsic Function)
     move function numval(ws-alpha1) to ws-number
     display "Number = " ws-number
***** Use .Net to do the same. The Parse Method is available on
***** the numeric data classes.
     move decimal::Parse(ws-str1) to ws-number  *> HERE ws-number = (+23412.00) when it should be (+1234.12)
     display "Number from .Net = " ws-number
***** Use the TryParse Method (Allows you to trap invalid numerics)
     if not decimal::TryParse(ws-str1 , ws-decimal)
     display "Wrong"
     end-if
     display "Decimal = " ws-decimal
     move ws-decimal to ws-number
     display "Number = " ws-number
***** Show an Invalid
     set ws-str1 to "DuffNumber"
     if not decimal::TryParse(ws-str1 , ws-decimal)
     display "Wrong"
     end-if
     stop "Press Return to Terminate"   
     goback
 end program Program1.

Microsoft Visual Studio 2015 Community
Version 14.0.24720.00 Update 1
Microsoft .NET Framework
Version 4.6.01038
Micro Focus Visual COBOL 2.3 Version 2.3.00343.

0 Likes
1 Solution

Accepted Solutions
Altair Absent Member.
Absent Member.

RE: Error_StringToDecimalParse

Jump to solution

Was missing declare the clause: special-names. decimal-point is comma.

0 Likes
6 Replies
Altair Absent Member.
Absent Member.

RE: Error_StringToDecimalParse

Jump to solution

Was missing declare the clause: special-names. decimal-point is comma.

0 Likes
MikePolinske
Visitor.

RE: Error_StringToDecimalParse

Jump to solution

When I run it with special-names. decimal-point is comma I get the following results:

Number = +01234.12

Number from .Net = +00012.34

Decimal = 1234.12

Number = +00012.34

Wrong

But, when I run it without the special-names paragraph, it seems to come out correctly:

Number = +01234.12

Number from .Net = +01234.12

Decimal = 1234.12

Number = +01234.12

Wrong

0 Likes
Highlighted
Altair Absent Member.
Absent Member.

RE: Error_StringToDecimalParse

Jump to solution

Hi, MikePolinske,

Well, when I insert special-names the result is:

Number = +01234.12

Number from .Net = +01234.12

Decimal = 123412

Number = +01234.12

WithOut special-names the result is:

Number = +01234.12

Number from .Net = +23412.00

Decimal = 123412

Number = +23412.00

 

Wrong.

0 Likes
MikePolinske
Visitor.

RE: Error_StringToDecimalParse

Jump to solution

Very odd.  Are you running on Windows 10?  Otherwise, it looks like we are both on the same version of Visual Studio and Visual Cobol.

0 Likes
Altair Absent Member.
Absent Member.

RE: Error_StringToDecimalParse

Jump to solution

Windows 10 Home

Microsoft Visual Studio 2015 Community

Version 14.0.24720.00 Update 1

Microsoft .NET Framework

Version 4.6.01038

Micro Focus Visual COBOL 2.3 Version 2.3.00343.

0 Likes
RobertSales Absent Member.
Absent Member.

RE: Error_StringToDecimalParse

Jump to solution

Isn't the difference here about how the system culture is set up?  The documentation for the Parse method says:

Parameter s is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture. For more information, see CurrentInfo. To parse a string using the formatting information of some other culture, use the Decimal.Parse(String, IFormatProvider) or Decimal.Parse(String, NumberStyles, IFormatProvider) method.

If the system culture is set up to a culture in which the thousands separator is '.' and the decimal separator is ',', then effectively any '.' character in the input string will be ignored.  So it is expected behaviour that the statement:

    move decimal::Parse(ws-str1) to ws-number  *> HERE ws-number = (+23412.00)

...will produce the answer +23412.00 when the input string is  1234.12 (since the leading digit will be truncated).

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.