Re: Numeric rounding not working?
From: Steve Kass (skass_at_drew.edu)
Date: 10/21/04
- Next message: Adam Machanic: "Re: Free SQL Server Tools"
- Previous message: Eric Garza: "Re: closest entry to a particular time"
- In reply to: adude: "Numeric rounding not working?"
- Next in thread: adude: "Re: Numeric rounding not working?"
- Reply: adude: "Re: Numeric rounding not working?"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 20 Oct 2004 20:02:02 -0400
Edward,
The data type decimal holds zero digits after the decimal point, so
your four variables will have the values 27, -80, 27, and -80
respectively. The distance between the points is then zero, though you
see a result of a few inches because of the non-exact trig calculations.
If you change the data type for the coordinates to float, or to a
decimal type like decimal(20,16), you'll get an answer of about 9.22
miles. I think that's the correct answer, not 6. You might also want
to use the built in RADIANS function for more precision than you get
from 57.2958.
declare @lat1 as float
declare @lon1 as float
declare @lat2 as float
declare @lon2 as float
set @lat1 = radians(26.7174)
set @lon1 = radians(-80.0695)
set @lat2 = radians(26.5956)
set @lon2 = radians(-80.1302)
declare @EquationResult as Float
set @EquationResult = 3963 * acos(sin(@lat1) *
sin(@lat2) + cos(@lat1) * cos(@lat2) *
cos(@lon2 - @lon1))
select @EquationResult as [Original Result]
Steve Kass
Drew University
adude wrote:
>I am trying to get the result of an equation (which has many decimal
>places) to round to the nearest whole number. But everytime I try
>casting it to round I get a zero.
>
>Here is the sql script:
>-----------------------
>declare @lat1 as decimal
>declare @lon1 as decimal
>declare @lat2 as decimal
>declare @lon2 as decimal
>
>set @lat1 = 26.7174
>set @lon1 = -80.0695
>set @lat2 = 26.5956
>set @lon2 = -80.1302
>
>declare @EquationResult as Float
>set @EquationResult = 3963 * acos(sin(@lat1/57.2958) *
>sin(@lat2/57.2958) + cos(@lat1/57.2958) * cos(@lat2/57.2958) *
>cos(@lon2/57.2958 - @lon1/57.2958))
>select @EquationResult as [Original Result]
>
>select cast(@EquationResult as decimal(1)) as [Error Result]
>select cast(5.6053301811218262 as decimal(1)) as [Almost Correct]
>
>RESULTS
>========
>Original Result: 5.9053301811218262E-5
>Error Result: 0
>Almost Correct: 6
>
>-------------------
>If I try casting the @EquationResult variable that has the "Original
>Result" value of 5.9053301811218262E-5 then I get "Error Result" 0 which
>is not the rounding I was expecting to happen.
>
>I noticed that if I remove the E-5 from the "Original Result" then I try
>casting it again then I get the "Almost Correct" value that I am looking
>for. I say "Almost Correct" because I have not been able to figure out a
>way to take the "Original Result" value (@EquationResult) and round from
>there.
>
>Help!
>
>Sincerely,
>
>Edward Burns
>
>
>
>
>*** Sent via Developersdex http://www.developersdex.com ***
>Don't just participate in USENET...get rewarded for it!
>
>
- Next message: Adam Machanic: "Re: Free SQL Server Tools"
- Previous message: Eric Garza: "Re: closest entry to a particular time"
- In reply to: adude: "Numeric rounding not working?"
- Next in thread: adude: "Re: Numeric rounding not working?"
- Reply: adude: "Re: Numeric rounding not working?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|