Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Convert float to char
Select Cast('100.1234' as float)
give me the result 100.1234
Now when I convert it back to char I want exactly 100.1234
Select  Convert(char(100),Ca
st('100.1234' as float))
Gives me 100.123 (Here I was expecting 100.1234)

When I do
Select STR(Cast('100.1234' as float),25,4)
I get back the result as 100.1234

However here I am not sure how many digits do I have after the decimal
point. If I put some value like
Select STR(Cast('100.1234' as float),25,8)
I get 0's appended to it, which is again not desired.



Thanks in advance,
Jai


Report this thread to moderator Post Follow-up to this message
Old Post
jaijai_kumar@hotmail.com
08-27-05 01:23 AM


Re: Convert float to char

 jaijai_kumar@hotmail
.com wrote:

> Select Cast('100.1234' as float)
> give me the result 100.1234
> Now when I convert it back to char I want exactly 100.1234
> Select  Convert(char(100),Ca
st('100.1234' as float))
> Gives me 100.123 (Here I was expecting 100.1234)
>
> When I do
> Select STR(Cast('100.1234' as float),25,4)
> I get back the result as 100.1234
>
> However here I am not sure how many digits do I have after the decimal
> point. If I put some value like
> Select STR(Cast('100.1234' as float),25,8)
> I get 0's appended to it, which is again not desired.
>
>
>
> Thanks in advance,
> Jai

Hi Jai. I want to warn you about going down this path.
You will never get exactly what you want all the time,
because there is a base converson from base 10 to binary,
and there are lots of distressingly simple-seeming
decimal numbers that do not convert exactly to a binary
float. Then converting back will show the loss by giving
a different value. I suggest using a DECIMAL datatype
if you want to retain exactitude...

Joe Weinstein at BEA Systems


Report this thread to moderator Post Follow-up to this message
Old Post
Joe Weinstein
08-27-05 01:23 AM


Re: Convert float to char
(jaijai_kumar@hotmai
l.com)  writes:
> Select Cast('100.1234' as float)
> give me the result 100.1234
> Now when I convert it back to char I want exactly 100.1234
> Select  Convert(char(100),Ca
st('100.1234' as float))
> Gives me 100.123 (Here I was expecting 100.1234)
>
> When I do
> Select STR(Cast('100.1234' as float),25,4)
> I get back the result as 100.1234
>
> However here I am not sure how many digits do I have after the decimal
> point. If I put some value like
> Select STR(Cast('100.1234' as float),25,8)
> I get 0's appended to it, which is again not desired.

Since a float is an approximate number, this is not any exact science.
Consider:

SELECT cast('100.1235' as float)

this gives in Query Analyzer:

100.12350000000001

So when you convert it to string, which value do you want?

Anyway, here is a horrible expression that achieves what you are looking
for. But note the caveate above, and be aware that you may not always
get what you want.

Select  reverse(substring(x,
 patindex('%[^0]%', x), 25))
from  (Select x =  reverse(ltrim(str(Ca
st('100.1235' as float), 25, 8)))) y




--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp


Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
08-27-05 01:23 AM


Re: Convert float to char
Here is another horrible expression, that does the same thing:

select  replace(rtrim(replac
 e(ltrim(str(Cast('10
0.1235' as float),
25, 8)),'0',' ')),' ','0')

Razvan


Report this thread to moderator Post Follow-up to this message
Old Post
Razvan Socol
08-28-05 08:23 AM


Re: Convert float to char
Hello all,

Thanks a lot for all your messages.
Erland your work around gave me a good hint.
I have created a small function to do the same.

Jai
 jaijai_kumar@hotmail
.com  wrote:
> Select Cast('100.1234' as float)
> give me the result 100.1234
> Now when I convert it back to char I want exactly 100.1234
> Select  Convert(char(100),Ca
st('100.1234' as float))
> Gives me 100.123 (Here I was expecting 100.1234)
>
> When I do
> Select STR(Cast('100.1234' as float),25,4)
> I get back the result as 100.1234
>
> However here I am not sure how many digits do I have after the decimal
> point. If I put some value like
> Select STR(Cast('100.1234' as float),25,8)
> I get 0's appended to it, which is again not desired.
>
>
>
> Thanks in advance,
> Jai


Report this thread to moderator Post Follow-up to this message
Old Post
jaijai_kumar@hotmail.com
08-31-05 01:24 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 11:04 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006