How can I convert a float value to a string, without rounding issues or
ending up with scientific notation ?
Seems like a simple request. I have various types of numbers stored in a
Float field that I need to convert to a string as is. I don't want
rounding, nor do I want additional decimal places. I want the number as is.
And I can't use a different data type...I'm stuck using Float.
CAST( 1 to 999999) works fine.
CAST( > 999999) returns scientific notation. There's one I wasn't
expecting.
I don't necessarily know how many decimal places the number has, so if I use
STR(), I don't know how many decimal places to tell it. If I don't specify
number of decimal places, it rounds to nearest Integer. Not good.
I want (float) 12345678.90 to = (string) 12345678.90
Any ideas ? I've wracked my brain and others up till this point.
Thanks a bunch !
BillHave you read through this, yet?
http://msdn.microsoft.com/library/d...br />
2f3o.asp
ML
http://milambda.blogspot.com/|||Hi Bill,
I think you'll have to use str to convert it to a varchar - and then
manipulate that varchar as you require. Something like this...
--function
drop function dbo.fnFloatToString
go
create function dbo.fnFloatToString(@.x float) returns varchar(50) as
begin
declare @.s varchar(50)
select @.s = rtrim(ltrim(reverse(substring(z, patindex('%[^.]%', z),
50)))) from
(select substring(y, patindex('%[^0]%', y), 50) as z from
(select reverse(str(@.x, 40, 20)) as y) a) b
return @.s
end
go
--data
declare @.t table (x float)
insert @.t
select 1
union all select 123
union all select 12345678
union all select 123456789012345
union all select 1.2
union all select 1.234567
union all select 1.2345678901234
--results
select x, dbo.fnFloatToString(x) from @.t
"ML" wrote:
> Have you read through this, yet?
> http://msdn.microsoft.com/library/d... />
o_2f3o.asp
>
> ML
> --
> http://milambda.blogspot.com/
Showing posts with label orending. Show all posts
Showing posts with label orending. Show all posts
Subscribe to:
Posts (Atom)