Solved

Separate Float into Integer and Fractional Parts

Posted on 2011-03-22
4
4,403 Views
Last Modified: 2012-05-11
I need to separate a floating point decimal into 2 integers, one that represents the whole number part, and one that represents the factional part.  For example, if the float is 10.25, I need to get 10 and 25.

The first part is easy, CONVERT(INT, [floating number])

However, I can't figure out the fractional part because there can be any number of digits after the decimal.

Both [floating number]%(CONVERT(INT, [floating number])) and [floating number] - (CONVERT(INT, [floating number])) will remove the whole number part, leaving me with 0.25 in my example.  My challenge is scaling 0.25 up to 25, without knowing that there are only 2 digits after the decimal.

I hope that's not too repetitive.  Any help would be much appreciated.  Thanks.
0
Comment
Question by:DBag1
  • 2
4 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 35191364
for positive numbers, including whole numbers

--works for positive numbers only, for negative would need some thought as to
-- how it should work

DECLARE @VAL as float;
SET @VAL = 3.14159265;
--SET @VAL = 3;
DECLARE @DEC AS varchar(50);
SET @DEC = CAST((@VAL - FLOOR(@VAL)) AS VARCHAR);
IF CHARINDEX('.', @DEC) > 0
  SET @DEC = SUBSTRING(@DEC, CHARINDEX('.',@DEC) + 1, LEN(@DEC) - CHARINDEX(@DEC,'.')); 



SELECT CAST(FLOOR(@VAL) AS INTEGER) AS THEINT, @DEC THEREST

Open in new window

0
 
LVL 26

Expert Comment

by:tigin44
ID: 35191367
DECLARE @N1  int
DECLARE @N2  int

SELECT @N1 = ROUND([floating number],0)
SELECT @N2 = ROUND([floating number] - @N1, 2)

0
 
LVL 18

Accepted Solution

by:
deighton earned 500 total points
ID: 35191413
--works for -ve numbers

DECLARE @VAL as float;
SET @VAL = -27.3344333;
--SET @VAL = 3;

DECLARE @SGN AS INTEGER;
SET @SGN = 1;
IF @VAL<0 
 SELECT @SGN = -1, @VAL = 0 - @VAL;

DECLARE @DEC AS varchar(50);
SET @DEC = CAST((@VAL - FLOOR(@VAL)) AS VARCHAR);
IF CHARINDEX('.', @DEC) > 0
  SET @DEC = SUBSTRING(@DEC, CHARINDEX('.',@DEC) + 1, LEN(@DEC) - CHARINDEX(@DEC,'.')); 



SELECT @SGN * CAST(FLOOR(@VAL) AS INTEGER) AS THEINT, @DEC THEREST

Open in new window

0
 

Author Closing Comment

by:DBag1
ID: 35191482
Thanks!!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

758 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

23 Experts available now in Live!

Get 1:1 Help Now