0

I have a SQL CLR function that receives a SQLString, and I am converting the Sql string to a c# string with .ToString(), then after some computing I want to return a sring that contains doubles

   [Microsoft.SqlServer.Server.SqlFunction]
   public static string FuncCS(SqlString SQLstr){
   string CSstr = SQLstr.ToString();

     /*DO SOME STUFF*/
    for (int j = 0; j < S.ColumnCount; j++){
        for (int i = 0; i < S.RowCount; i++){
            CSstr += S[i, j].ToString("F5") + " ,  \t"; 
        }           
    }
  }

Then in SQL I receive a string but the doubles lose their decimal point and is substituted by a misplaced comma:

RESULT:

149,50625 0,00000 0,00000 0,00000 0,00000 69,06880 0,00000 0,00000 0,00000 0,00000 31,61588 0,00000 0,00000 0,00000 0,00000 8,82157 

WHAT IS NEEDED:

14.9506245839579,0,0,0,0,6.90687968992126,0,0,0,0,3.16158756810842,0,0,0,0,0.88215732592823

What do I change in the for loop?

1

2 Answers 2

4

I would imagine "culture"; try using ToString("F5", CultureInfo.InvariantCulture)

Also - look at StringBuilder for building a string in a loop.

Sign up to request clarification or add additional context in comments.

9 Comments

I am getting incorrect results, After doing what you suggested I get: 149.50625, 0.00000, 0.00000, 0.00000, 0.00000, 69.06880, 0.00000, 0.00000, 0.00000, 0.00000, 31.61588, 0.00000, 0.00000, 0.00000, 0.00000, 8.82157, instead 14.9506245839579,0,0,0,0,6.90687968992126,0,0,0,0,3.16158756810842,0,0,0,0,0.88215732592823 is like it would be multiplying by ten each double
@cMinor well, what are the inputs here? Is the first number 14(ish) or 149(ish)?
@cMinor also, if you didn't want fixed precision, why did you choose F5?
the input would be 14.9506245839579, but getting 149.50625
@cMinor probably "R" (round-trip). Alternatively, "G" (general)
|
1

What is the result if you just use

CSstr += S[i, j] + " ,  \t";

?

2 Comments

the result is: 149,506245839579 , 0 , 0 , 0 , 0 , 69,0687968992126 , 0 , 0 , 0 , 0 , 31,6158756810842 , 0 , 0 , 0 , 0 , 8,82157325928232 ,
try double d = Double.Parse(S[i, j], new CultureInfo("de-DE")); Question: What culture do you have in your db and client? Are you sure the values are stored correctly in the db?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.