Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Float Rounding when converted to varchar

Posted on 2013-06-05
Medium Priority
452 Views
When converting a value from a Float to a varchar, it's rounding.  For example, I have a value, \$35140.08, and when converted to a varchar it becomes 35140.1.  My ultimate goal is I need to present the value (which is a float datatype) with 2 decimal places with no decimal point, ie: 3514008.

``````SELECT REPLACE(CAST(CAST('35140.08' as FLOAT) as VARCHAR(255)),'.','') as MyValue
``````

Any ideas?  I tried FLOOR, STR and neither helped.
0
Question by:lm1189
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 25

Expert Comment

ID: 39221868
SELECT REPLACE(CAST(CAST('35140.08' as numeric (24, 2)) as VARCHAR(255)),'.','') as MyValue
0

LVL 49

Expert Comment

ID: 39221885
declare @f as float
set @f = 35140.088888888888

SELECT CAST(CAST(@f * 100 as bigint) as VARCHAR(255)) as MyValue
0

LVL 25

Expert Comment

ID: 39221900
Like it...

Even simpler:

SELECT cast(35140.08 * 100 as bigint)
0

LVL 49

Expert Comment

ID: 39221904
:) I like simple
0

LVL 70

Expert Comment

ID: 39222142
What if the value internally is: \$35140.086?

Do you want to see 3514008 or 3514009?
0

LVL 75

Expert Comment

ID: 39224234
When converting a value from a Float to a varchar, it's rounding.
Just as an aside and I am sure you have already figured this out, but float is an approximate numeric value and typically should not be used in business applications.  Instead you should use numeric or money as they are fixed numeric types and therefore no rounding problems like this.
0

LVL 49

Accepted Solution

PortletPaul earned 2000 total points
ID: 39224411
[float]>>should not be used in business applications
I think it's actually forbidden in some places by regulation (but law is not my strong suit)
0

LVL 49

Expert Comment

ID: 39258977
lm1189, Hi. Have we answered this question - could it be closed off?
0

Author Closing Comment

ID: 39259304
I tried multiple methods but all had rounding issues, I eventually had to conver to the Money datatype which resolved my issues.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.