Solved

Separate Float into Integer and Fractional Parts

Posted on 2011-03-22
4
4,665 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
[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
  • Learn & ask questions
  • 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

724 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