473,383 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Splitting double into integers

Hi,

How can I split Double into two integers like the following example :

2.34 -> 2 and 34 (I dont want the modf() result that gives 2 and 0.34)

Hugo
Nov 23 '05 #1
3 998
Well, first off, how are you going to deal with trailing zeros? Note that "2
and 34" is the same as "2 and 340". I'll assume you don't want trailing
zeros.

You say you can get it to where you've got "2 and .34". Ok, you're more than
half way there. You want to know the number of digits in the fractional
part, and then multiply it by 10 to that power. To get the number of digits
is easy:

int Digit_Count( double value )
{
return (value.ToString())->Length ;
}

Note this will include the decimal point, so subtract one from this. So, you
want to do this (using your example):

34 = .34 * pow( 10, Digit_Count( .34 ) - 1 ) ;

Now just use variables where the "34" and ".34" are...

[==P==]

"H.B." <HB@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
Hi,

How can I split Double into two integers like the following example :

2.34 -> 2 and 34 (I dont want the modf() result that gives 2 and 0.34)

Hugo

Nov 23 '05 #2
How about this:

double f = 2.34;
int whole = (int) f;
int decimal = (f - whole) * 100;

Cheers,
Mark

"Peter Oliphant" wrote:
Well, first off, how are you going to deal with trailing zeros? Note that "2
and 34" is the same as "2 and 340". I'll assume you don't want trailing
zeros.

You say you can get it to where you've got "2 and .34". Ok, you're more than
half way there. You want to know the number of digits in the fractional
part, and then multiply it by 10 to that power. To get the number of digits
is easy:

int Digit_Count( double value )
{
return (value.ToString())->Length ;
}

Note this will include the decimal point, so subtract one from this. So, you
want to do this (using your example):

34 = .34 * pow( 10, Digit_Count( .34 ) - 1 ) ;

Now just use variables where the "34" and ".34" are...

[==P==]

"H.B." <HB@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
Hi,

How can I split Double into two integers like the following example :

2.34 -> 2 and 34 (I dont want the modf() result that gives 2 and 0.34)

Hugo


Nov 23 '05 #3
H.B. wrote:
Hi,

How can I split Double into two integers like the following example :

2.34 -> 2 and 34 (I dont want the modf() result that gives 2 and 0.34)


Do you want 2.340000001 to be 2 and 340000001 or 2 and 34? E.g. do you
specifically want the two integers to be units and hundreths, or
hundreths and, basically, the string representation of the fractional
part turned into an int? What about range issues, for example if your
number is outside the range of int (double has a huge range compared to
int)?

Why do you need the split anyway?

Tom
Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: jose luis fernandez diaz | last post by:
Hi, My OS is: cronos:jdiaz:tmp>uname -a HP-UX cronos B.11.11 U 9000/800 820960681 unlimited-user license I compile in 64-bits mode the program below:
22
by: Fred Ma | last post by:
I'm using the expression "int a = ceil( SomeDouble )". The man page says that ceil returns the smallest integer that is not less than SomeDouble, represented as a double. However, my...
14
by: cj | last post by:
VB2003. I need a large positive integer. Which is larger int64 or double? I see int64 also apparently is known as long and will hold -9,223,372,036,854,775,808 through...
4
by: Michael Yanowitz | last post by:
Hello: For some reason I can't figure out how to split a 4-byte (for instance) float number (such as 3.14159265359) into its 4-bytes so I can send it via a socket to another computer. For...
11
by: Bo Peng | last post by:
Dear C++ experts, I need to store and retrieve a meta information that can be int or double. The program would be significantly simpler if I can handle two types uniformly. Right now, I am using...
6
by: Johs | last post by:
I have: int a = 1; int b = 2; double c = a/b; Is it somehow possible to divide these two integers and get the result as a double 0.5? Or do they both have to be declared as doubles?
6
by: Alexander Stoyakin | last post by:
Hello, please advise on the following issue. I need to check that difference between two double values is not higher than defined limit. int main() { double limit = 0.3; double val1 = 0.5,...
4
by: yogi_bear_79 | last post by:
I have a simple string (i.e. February 27, 2008) that I need to split into three parts. The month, day, and year. Splitting into a string array would work, and I could convert day and years to...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.