Tuesday, March 20, 2012

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.

- SaratOriginally 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|||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.

No comments:

Post a Comment