Error_StringToDecimalParse

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.

  • Verified Answer

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

  • 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

  • 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.

  • 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.

  • 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.

  • 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).