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!

Round up to 8 sig fig

Status
Not open for further replies.

johnnymagee69

Programmer
Sep 4, 2006
9
US
Hi,
I have to re-create in SQL a custom Excel function which rounds numbers to 8 significant figures (because thats how many digits can be shown on a calculator!). I can work out the number of decimal points correctly but the round() function then rounds down when called in a procedure instead of up as it does normally when the digit to truncate is 5. All numbers are type decimal(38,33).

eg
The number to round up is 20.6348035.
When called in a procedure: round(20.6348035,6)=20.6348030 but outside of a procedure: round(20.6348035,6)=20.6348040

Any help appreciated.

Ta



 
Try using Round() in conjunction with CEILING(). You'll have to be careful as Ceiling() will try to round the whole number, decimals included, but I'm sure you can work around it.



Catadmin - MCDBA, MCSA
"No, no. Yes. No, I tried that. Yes, both ways. No, I don't know. No again. Are there any more questions?"
-- Xena, "Been There, Done That"
 
Yeah, also tried this but whilst it worked for this example it fails for others (eg fn_round(1.12345678)=1.123456):

create function fn_roundup(@num decimal(38,33),@dp int)
returns decimal(38,33)
as begin
if @dp <0 set @dp=0
return @ceiling(@num*power(10,@dp)) / power(10,@dp)
end
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top