Solved

Float Rounding when converted to varchar

Posted on 2013-06-05
9
440 Views
Last Modified: 2013-06-19
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

Open in new window


Any ideas?  I tried FLOOR, STR and neither helped.
0
Comment
Question by:lm1189
9 Comments
 
LVL 25

Expert Comment

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

Expert Comment

by:PortletPaul
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

by:Lee Savidge
ID: 39221900
Like it...

Even simpler:

SELECT cast(35140.08 * 100 as bigint)
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39221904
:) I like simple
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 69

Expert Comment

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

Do you want to see 3514008 or 3514009?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
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 48

Accepted Solution

by:
PortletPaul earned 500 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 48

Expert Comment

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

Author Closing Comment

by:lm1189
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now