Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding in functions

Status
Not open for further replies.

johnnymagee69

Programmer
Sep 4, 2006
9
US
Hi, I'm getting some strange answers when using round().

If I just use a single query select round(20.6348035,6) correctly rounds up and returns 20.634804. However when called as part of a stored procedure round(20.6348035,6) rounds down and returns 20.634803. Has anyone got any idea why round() returns different answers?
 
Did you pass 3th parameter to the ROUND() in SP?
Code:
round(20.6348035,6,1) -> 20.6348030
round(20.6348035,6)   -> 20.6348040

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Hi bborisov,
I wasnt using a 3rd parameter. I just tried this but it still rounds down regardless of whether the 3rd parameter is 0 or 1 .
 
Can you post the stored procedure?

Good Luck,
Alex

Ignorance of certain subjects is a great part of wisdom
 
I'm not sure this will help much but...

(ps dbo.fn_eroundfactor returns the number of places to round by)

Code:
ALTER procedure [dbo].[sp_NGprices_DONGd](@dongpriceindex varchar(32))
as begin
declare @pricemonth datetime
declare @priceday datetime
declare @surrogate int
declare @Iindex varchar(32)
declare @Lindex varchar(32)
declare @DCindex varchar(32)
declare @Pconstant float
declare @Cconstant float
declare @DCconstant float
declare @Iconstant float
declare @Lconstant float
declare @Iweight float
declare @ITweight float
declare @Lweight float
declare @DCweight float
declare @Cweight float
declare @DCavg float
declare @Iavg float
declare @Lavg float
declare @I float
declare @L float
declare @DC float
declare @ILDC float
declare @C float
declare @Pn float
declare @price float
declare @constants table(constant varchar(32),description varchar(32),value float)
declare @calc table(surrogate int identity(1,1),pricedate datetime,price float)
--GET CONSTANTS
insert into @constants(constant,description,value)
select constant,description,value
from dongpriceconstant
where dongpriceindex=@dongpriceindex
set @Pconstant= (select value from @constants where constant='Po')
set @Cconstant= (select value from @constants where constant='Co')
set @DCconstant= (select value from @constants where constant='DCo')
set @Iconstant= (select value from @constants where constant='Io')
set @Lconstant= (select value from @constants where constant='Lo')
set @Cweight= (select value from @constants where constant='Weight Co')
set @DCweight= (select value from @constants where constant='Weight DC')
set @Iweight= (select value from @constants where constant='Weight I')
set @ITweight= (select value from @constants where constant='Weight I (t)')
set @Lweight= (select value from @constants where constant='Weight L')
set @DCindex= (select description from @constants where constant='DC Index')
set @Iindex= (select description from @constants where constant='I Index')
set @Lindex= (select description from @constants where constant='L index')
--GET START DATE
set @pricemonth=(select dateadd(day,1,max(pricedate)) from pricevalue where priceindex=@dongpriceindex)
--FOR EACH QUARTER
while @pricemonth<=getdate()
	begin
		--CALCULATE THE PRICE
		set @Iavg=dbo.fn_DONGAvgPrice(@Iindex,@pricemonth,6,3)
		set @Iavg=round(@Iavg,dbo.fn_eroundfactor(@Iavg))
		set @Lavg=dbo.fn_DONGAvgPrice(@Lindex,@pricemonth,6,3)
		set @Lavg=round(@Lavg,dbo.fn_eroundfactor(@Lavg))
		set @DCavg=dbo.fn_DONGAvgPrice(@DCindex,@pricemonth,6,3)
		set @DCavg=round(@DCavg,dbo.fn_eroundfactor(@DCavg))
		set @I = round(@Iavg/@Iconstant,dbo.fn_eroundfactor(@Iavg/@Iconstant))
		set @I = round(@Iweight*@I,dbo.fn_eroundfactor(@Iweight*@I))
		set @L = round(@Lavg/@Lconstant,dbo.fn_eroundfactor(@Lavg/@Lconstant))
		set @L = round(@Lweight*@L,dbo.fn_eroundfactor(@Lweight*@L))
		set @DC = round(@DCavg/@DCconstant,dbo.fn_eroundfactor(@DCavg/@DCconstant))
		set @DC = round(@DCweight*@DC,dbo.fn_eroundfactor(@DCweight*@DC))
		set @ILDC = round(@I+@L+@DC,dbo.fn_eroundfactor(@I+@L+@DC))
		set @Pn = round(@Pconstant*@ILDC,dbo.fn_eroundfactor(@Pconstant*@ILDC))
		set @I = round(@Iavg/@Iconstant,dbo.fn_eroundfactor(@Iavg/@Iconstant))
		set @I = round(@Cconstant*@ITweight*@I,dbo.fn_eroundfactor(@Cconstant*@ITweight*@I))
		set @C = round(@Cconstant*@Cweight,dbo.fn_eroundfactor(@Cconstant*@Cweight))
		set @C = round(@C+ @I,dbo.fn_eroundfactor(@C+ @I))
		set @price = @Pn-@C
		set @price = round(@Price,dbo.fn_eroundfactor(@Price))
		insert into DONGcalculationsD(pricemonth,Pconstant,Cconstant,DCconstant,Iconstant,Lconstant,Cweight,DCweight,Iweight,ITweight,Lweight,
			Iavg,Lavg,DCavg,Icalc,Lcalc,DCcalc,Ccalc,Pn,price,creationname,creationdate)
		values (@pricemonth,@Pconstant,@Cconstant,@DCconstant,@Iconstant,@Lconstant,@Cweight,@DCweight,@Iweight,@ITweight,@Lweight,
			@Iavg,@Lavg,@DCavg,@I,@L,@DC,@C,@Pn,@price,'Interface',getdate())

		set @pricemonth=dateadd(q,1,@pricemonth)
	end

end
 
You're right, that is a bit much for me to look at today.

Are you sure that your expected value to be rounded 20.6348035 is in fact this value, and not 20.63480348 or something along those lines?

And which particular request for rounding is the one that is giving you problems?

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top