4 Replies Latest reply: May 15, 2014 10:09 AM by Data Kruncher RSS

    Str Function

    joey

      I'm trying to work on a custom function to convert a number into a string that contains the digits of the number plus a sign at the end.  The str function to convert the number to a string is causing a hang up for me. 

       

      The problem with the string functions is one of the optional parameters is the number of decimal positions to round.   In my case, I want all decimal places kept, and I don't know the number of decimal places. 

       

      I can easily convert the integer portion of the number using str.  But how to convert the decimal places?

        • Str Function
          Data Kruncher

          So 12345.1234 would yield "9", 123.45 would yield "5", 12.0 would yield "2" (or would it be "3"?) and -123.1 would yield "4-"?

           

          If so, that's a nasty challenge, now isn't it. :confused:

            • Str Function
              joey

              Note quite.   And, as an additional requirement I was stripping the decimal. 12345.1234 would yied "123451234".  12.0 would be "120"  -123 would be "123-".   This is the burden of working with the mainframe.

               

              The more I thought about it, I had to force the function to pass the number of decimal places as an arguement.  If the model that calls the function doesn't pass the correct number, the results will be unpredictable (often zeros).

               

              I'd love to hear others solutions and ideas to get arround this though.

                • Str Function
                  Data Kruncher

                  I'm close, but it's not perfect. "#.0" is the problem. Here's what I've got so far...

                   

                  The value captured from the report is RptNum.

                   

                  StrDec represents the decimal portion of RptNum, using:

                  Replace(Intrim(replace(str((abs(RptNum-Int(RptNum)))*1000000000),"0"," "))," ","0")[/CODE]

                   

                  The final Result field uses:

                  Trim(Str(Abs(int(RptNum))))StrDecIf(RptNum<0,"-","+")[/CODE]

                   

                  Hopefully this moves you ahead somewhat.

                    • Str Function
                      Data Kruncher

                      A pretty straightforward fix for the 12.0 case:

                      Trim(Str(Abs(Int(RptNum))))If(Len(StrDec)=0,"0",StrDec)If(RptNum<0,"-","+")[/code]But if you need more decimal places than just the single forced significant digit, "0" above, then you could perhaps add a runtime parameter for the number of sigificant decimal places to use, and then build a lookup calculated field named FillZero:

                      1     0[/FONT]

                      2     00

                      3     000

                      4     0000

                      5     00000

                      6     000000[/code]Etcetera, as needed.

                       

                      A nice little Excel-like Repeat() function would have been good here, but that's not an option at the moment.

                       

                      Then the formula above would become:

                      Trim(Str(Abs(Int(RptNum))))+

                      If(Len(StrDec)=0,FillZero,StrDec)+

                      If(RptNum<0,"-","+")[/code]