|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Query - General - Round off a Real Value without Truncation
Hi,
I want to perform the following steps 1. Convert the real number into a character. 2. Convert the resulting character to a decimal. 3. Round the value at the N+1 th place. My requirement is When n=2 I want to round 18.005 to 18.01 or 18.0049 to 18.00(at 100th place) when i try with Declare @real real Set @real=18.005 print Round(Convert(Decimal(15,7),Convert(varchar(30),@r eal)),2) i can able to get the result until i get some indefinite real value like -4.8892562E-08. so when i try the same logic, i am getting a conversion error(Error converting data type varchar to numeric.). I noticed when i convert a real value of 19.05 to decimal(15,7) some of the digits are gets truncated. i am getting 19.0049991608 as result. Please help me to get the problem resolved. thanks in advance |
|
#2
|
||||
|
||||
|
Are you taking this approach because the number of decimal places you need to round to is variable? If that is not the case you can simply use decimal(15,2) for two decimal places and that will solve your problem; although I have a hunch you want something more variable.
So I tried working with your example using float instead of real and here is what I did: Code:
DECLARE @Num float
SET @Num = 18.005
DECLARE @DecPlaces int
SET @DecPlaces = 2
SELECT CAST(@Num as decimal(15,7))
DECLARE @SQLStr varchar(8000)
SET @SQLStr = 'SELECT CAST(' + CAST(@Num as varchar(50)) + ' as decimal(15,' + CAST(@DecPlaces as varchar(2)) + '))'
PRINT @SQLStr
EXEC (@SQLStr)
Give that a try and let me know if it works for you ![]()
__________________
Slarentice (origin:Shadow Wizard of ASP Free) [noun] A slave and apprentice of the Wizard's Circle (specifically of mehere) at ASP Free. ---- If shemzilla takes over, it's best to be on his good side ![]()
|
|
#3
|
|||
|
|||
|
Re: Round Off a real value without truncation
Hi,
I hope that the below query will be useful for you Declare @real real,@n int, @sqlstr varchar(8000); Set @real=18.005; set @n = 1; set @sqlstr = 'Select cast(' + cast(@real as varchar)+' as decimal (5,' + cast(@n as varchar)+'))' exec (@sqlstr) Thanks & Regards Sakthimeenakshi.S |
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > Query - General - Round off a Real Value without Truncation |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|