# Thread: Converion for VARCHAR to FLOAT

1. Registered User
Join Date
Jun 2003
Location
Ohio
Posts
149

## Unanswered: Converion for VARCHAR to FLOAT

Hi-

I am trying the following example.

create table mytest (a float, b float(8))

declare @a FLOAT
declare @b varchar(10)

set @b = '0.4'

set @a = @b

PRINT @a

The result is 0.40000000000000002.

Can some one tell me what am I doing wrong? Appreciate your time.

- Sarat

2. Registered User
Join Date
Jan 2003
Location
India
Posts
523

## Re: Converion for VARCHAR to FLOAT

Originally posted by sbaru
Hi-

I am trying the following example.

create table mytest (a float, b float(8))

declare @a FLOAT
declare @b varchar(10)

set @b = '0.4'

set @a = @b

PRINT @a

The result is 0.40000000000000002.

Can some one tell me what am I doing wrong? Appreciate your time.

- Sarat
why do u need the table? what r u trying to do?

declare @a FLOAT
declare @b varchar(10)

set @b = '0.4'

set @a = @b
PRINT @a

it prints '0.4' for me

3. Registered User
Join Date
Feb 2004
Posts
4

## Re: Converion for VARCHAR to FLOAT

Originally posted by sbaru
Hi-

I am trying the following example.

create table mytest (a float, b float(8))

declare @a FLOAT
declare @b varchar(10)

set @b = '0.4'

set @a = @b

PRINT @a

The result is 0.40000000000000002.

Can some one tell me what am I doing wrong? Appreciate your time.

- Sarat
Yes, this is a common problem with all computers. When you declare float, the computer represents the numbers internally as approximations. The approximation is really really close to your number but often not exact.

To avoid this when working with monetary values, for example, I frequently will declare the field to be integer and just think of the value as pennies (for U.S. currency) and multiply the result by 100 to get dollars.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•