3

I have something like that in code

SqlFunctions.StringConvert( (double?) x.Latitude)

but it returns an integer always although it has a latitude value.

any help?

Mohamed Naguib
  • 1,720
  • 6
  • 23
  • 32

1 Answers1

3

SqlFunctions.StringConvert(double?) returns an integer converted to string. To return a decimal value, you need to provide the other overload SqlFunctions.StringConvert(double? vaue, int length, int decimals) as follows:

SqlFunctions.StringConvert( (double?) x.Latitude, 20, 5)

// If x.Latitude = 34.75, then the result will be "34.75000"

From the STR documentation:

The number is rounded to an integer by default or if the decimal parameter is 0.

References:
SqlFunctions.StringConvert Method (Nullable)
SqlFunctions.StringConvert Method (Nullable, Nullable, Nullable)
STR (Transact-SQL)

Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29
  • If i did that it adds zeros on the right and I don't want this to happen – Mohamed Naguib Feb 12 '14 at 16:12
  • A possible alternative is to simply use `.ToList()` at the end of your Linq query, and then do the formatting using Linq to Objects with `x.Latitude.ToString()`. – Wagner DosAnjos Feb 12 '14 at 16:56
  • @wdosanjos Linq doesn't support the function .ToString() – Mohamed Naguib Feb 13 '14 at 08:19
  • Linq-to-Entities (EF) does not support it, but Linq-to-Objects does. So I'm suggesting using a "greedy" method like `.ToList()` to hydrate all the object only then use `x.Latitude.ToString()`. If you provide your complete Linq query I can show you how. – Wagner DosAnjos Feb 13 '14 at 11:42
  • what if i dont want rounding to happen for e.g if i just want function to return '34' if input is '34.75' – Anshul Nigam May 22 '15 at 07:02