none
Mapping Issue

    Question

  • Hi,

    In my map I have to convert input number into number with four decimal number.

    Example: 1) 25.5 => 25.5000    2)23 => 23.0000   3)64.123456 => 64.1234

    I am trying to use string.Format function. In the scripting functoid I have following code:

    public string getValue(string DIV_RESULT)
    {
       if(DIV_RESULT.Length > 0)
       {
          string result = string.Format("{0:0.0000}", DIV_RESULT);
          return result;
       }
       else return "0";
    }

    The above code is not working as expected. For input 1.74 it is giving output as 1.74 only.

    Can someone help me on this. 


    Thanks, Girish R. Patil.

    Wednesday, June 04, 2014 12:13 PM

Answers

  • Ok, let's get this straightened out.

    All values in the Map are treated as String so numerical format strings won't work.  You have to (well, really, really should) convert to a numerical type first, then format appropriately.

    Here is a sample which I use all the time:

            private string formatTo4Dec(string number)
            {
                double dblNumber;
    
                if (double.TryParse(number, out dblNumber))
                {
                    return dblNumber.ToString("F4");
                }
                else
                {
                    throw new System.ArgumentException(number + " is not a valid number format.");
                }
            }
    

    You can handle the else condition any way you want. Sometime it's appropriate to just return "0.0000".

    Don't worry about any "performance" issues with parsing to a Double. It's for sure better than doing string manipulation and safer at runtime.

    • Proposed as answer by DeepthiAdith Wednesday, June 04, 2014 2:12 PM
    • Marked as answer by Girish R. Patil Thursday, June 05, 2014 7:34 AM
    Wednesday, June 04, 2014 2:03 PM
    Moderator

All replies

  • try with Math function

    System.Math.Round(DIV_RESULT, 4);

    i tried with Round functiod provided by BizTalk, it seems it cannot produce zeros, it can only round if there is value in decimals. 


    Please mark the post as answer if this answers your question. If this post is helpful, please vote as helpful by clicking the upward arrow mark next to my reply.

    Wednesday, June 04, 2014 12:20 PM
  • System.Math.Round(DIV_RESULT, 4);

    gave same output.

    For input 1.74 it is giving output as 1.74


    Thanks, Girish R. Patil.

    Wednesday, June 04, 2014 12:43 PM
  • Hi,

    Numbers don't have any conception of zeroes after a decimal point.

    But you can convert the number to a string format which represents the number with 0s after decimal point.

    Please refer "0" custom specifier in Custom Numeric Format Strings.

    Thanks,

    Deepthi


    DeepthiAdith

    Wednesday, June 04, 2014 12:59 PM
  • here is the pseudo code that you can use

    public string CheckDemicalPlaces(string param1)
    {
    	double noOfdecimalspresent=param1.Length-param1.IndexOf(".")-1;
            double zerosNeeded=4-noOfdecimalspresent;
            while(zerosNeeded>0)
            {
              param1= param1 + '0';
              zerosNeeded=zerosNeeded-1;
            }
    
    return  param1;
    }
    

     

    Please mark the post as answer if this answers your question. If this post is helpful, please vote as helpful by clicking the upward arrow mark next to my reply.

    Wednesday, June 04, 2014 1:14 PM
  • Ok, let's get this straightened out.

    All values in the Map are treated as String so numerical format strings won't work.  You have to (well, really, really should) convert to a numerical type first, then format appropriately.

    Here is a sample which I use all the time:

            private string formatTo4Dec(string number)
            {
                double dblNumber;
    
                if (double.TryParse(number, out dblNumber))
                {
                    return dblNumber.ToString("F4");
                }
                else
                {
                    throw new System.ArgumentException(number + " is not a valid number format.");
                }
            }
    

    You can handle the else condition any way you want. Sometime it's appropriate to just return "0.0000".

    Don't worry about any "performance" issues with parsing to a Double. It's for sure better than doing string manipulation and safer at runtime.

    • Proposed as answer by DeepthiAdith Wednesday, June 04, 2014 2:12 PM
    • Marked as answer by Girish R. Patil Thursday, June 05, 2014 7:34 AM
    Wednesday, June 04, 2014 2:03 PM
    Moderator
  • Excellent boatseller, thank you for making us aware of this approach.

    its been a good learning every time you drive us in better and right path.


    Please mark the post as answer if this answers your question. If this post is helpful, please vote as helpful by clicking the upward arrow mark next to my reply.

    Thursday, June 05, 2014 4:23 AM