Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Automatic Rounding?

I am very confused by the following results:

PRINT 3.1415926535897931 /180

Console result = 0.01745329251994329500

DECLARE @whatTheHell float(53)
SET @whatTheHell  = 3.1415926535897931/180
PRINT @whatTheHell 

Console result = 0.0174533

I don't understand because referring to this:

http://msdn.microsoft.com/en-us/library/ms131092.aspx

Sql Server Float should be equivalent to c# double. But when I compute this in c#:

double hellYeah = 3.1415926535897931 /180;

I get 0.017453292519943295...

like image 785
Roubachof Avatar asked Dec 20 '25 02:12

Roubachof


2 Answers

I think you're getting confused by the fact that PRINT implicitly converts numeric to character with the default setting for the STR function -- a length of 10 (see MSDN). Try PRINT STR(@wth, 20, 16) and you might be happier.

like image 168
Alex Martelli Avatar answered Dec 22 '25 18:12

Alex Martelli


Divide is not rounding. PRINT is rounding.

DECLARE
  @var1 float,
  @var2 float,
  @var3 float

SET @var1 = 3.1415926535897931
SET @var2 = 180

SET @var3 = @var1 / @var2
SELECT @var1/@var2 as Computed, @var3 as FromVariable

PRINT @var1/@var2
PRINT @var3
like image 25
Amy B Avatar answered Dec 22 '25 17:12

Amy B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!