473,324 Members | 1,678 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,324 software developers and data experts.

Help: Double convert to Integer and Double....

Hi Gents,

This is what I am trying to do.

Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the .5624 into a double or float?

The reason is because of converting decimal locations into DMS. Math
is simple....

14.5624 = 14 degrees .5624 minutes. Take the .5624 * 60 = 33.744 - 33
minutes .744 seconds. Then do it all over again so .744*60 = 44.64 -
45 seconds.

Basically, I do not know how to get the whole number out into an
integer variable and taking just the decimal place numbers into a
double or float.

Any help is appreciated.
Jul 22 '05 #1
5 8229
"da Vinci" <bl***@blank.com> wrote in message
news:cf********************************@4ax.com...
Hi Gents,

This is what I am trying to do.

Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the .5624 into a double or float?
The reason is because of converting decimal locations into DMS. Math
is simple....

14.5624 = 14 degrees .5624 minutes. Take the .5624 * 60 = 33.744 - 33
minutes .744 seconds. Then do it all over again so .744*60 = 44.64 -
45 seconds.

Basically, I do not know how to get the whole number out into an
integer variable and taking just the decimal place numbers into a
double or float.

Any help is appreciated.

#include <cmath>
#include <iomanip>
#include <iostream>

int main()
{
double value(14.5624);
double intpart(0);
double fracpart(modf(value, &intpart));

std::cout << std::right

<< std::setw(20) << "Value: "
<< value << '\n'

<< std::setw(20) << "Integer portion: "
<< intpart << '\n'

<< std::setw(20) << "Fractional portion: "
<< fracpart << '\n';

return 0;
}

-Mike
Jul 22 '05 #2
On Sat, 24 Jul 2004 03:41:19 GMT, da Vinci <bl***@blank.com> wrote:
Hi Gents,

This is what I am trying to do.

Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the .5624 into a double or float?

The reason is because of converting decimal locations into DMS. Math
is simple....

14.5624 = 14 degrees .5624 minutes. Take the .5624 * 60 = 33.744 - 33
minutes .744 seconds. Then do it all over again so .744*60 = 44.64 -
45 seconds.

Basically, I do not know how to get the whole number out into an
integer variable and taking just the decimal place numbers into a
double or float.

Any help is appreciated.


Well you are going about it the wrong way

14.5624 * 3600 = 52424.64 second ~ 52425 seconds

Now you have an integer

52425 / 3600 = 14 degrees, remainder 2025 seconds

2025 / 60 = 33 minutes, remainder 45 seconds

Use % to get the remainder of a division.

john
Jul 22 '05 #3
da Vinci wrote:
Hi Gents,

This is what I am trying to do.

Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the .5624 into a double or float?


Hi,
Integer can be received by using a cast to an integer.
The decimals are available by subtraction.
#include <iostream>
#include <cmath> // required for fabs()

using namespace std;
int main () {

double my_number=-14.5624;
int my_int = static_cast <int>(my_number); //with sign
// or: int my_int = (int)my_number;

double my_decimals = fabs(my_number-my_int); //signless

cout << "Number:" << my_number <<
"; Integer:" <<my_int<<
"; Decimals:" << my_decimals << endl;

return 0;
}

Number:-14.5624; Integer:-14; Decimals:0.5624

Regards marbac
Jul 22 '05 #4
marbac wrote:
da Vinci wrote:
Hi Gents,

This is what I am trying to do.

Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the .5624 into a double or float?

Hi,
Integer can be received by using a cast to an integer.
The decimals are available by subtraction.

Works this way, but as i saw, there is a more efficient method (modf()
in cmath).

http://www.cplusplus.com/ref/cmath/modf.html

regards marbac
Jul 22 '05 #5
On Sat, 24 Jul 2004 03:41:19 GMT, da Vinci <bl***@blank.com> wrote:
Hi Gents,

This is what I am trying to do.

Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the .5624 into a double or float?

The reason is because of converting decimal locations into DMS. Math
is simple....

14.5624 = 14 degrees .5624 minutes. Take the .5624 * 60 = 33.744 - 33
minutes .744 seconds. Then do it all over again so .744*60 = 44.64 -
45 seconds.

Basically, I do not know how to get the whole number out into an
integer variable and taking just the decimal place numbers into a
double or float.

Any help is appreciated.

Gents,

Thanks for the responses. Had a problem after I posted that and hadn't
been able to get on newsgroups.

I actually used the method of declaring an integer variable that was
equal to a defined double....

double dnum=25.625;
int inum = dnum

Since C++ rounds down, in every case I have tested, the integer has
always come out correctly. Then I just do a "dnum = dnum - inum" and
that gives me the integer I need and the double I need.

Appreciate the help gents.

Jul 22 '05 #6

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

Similar topics

1
by: Dr_Z2A | last post by:
Ok so a couple months ago I wrote a currency converter and never got it to compile (my memorization of syntax sucked then) and I just had the printed out copy lying in a stack of papers. So I...
18
by: James Radke | last post by:
Hello, We are currently using a user DLL that when working in VB 6.0 has a user defined type as a parameter. Now we are trying to use the same DLL from a vb.net application and are having...
36
by: Cap'n Ahab | last post by:
I have used VB3 - VB6, so learning all this OO stuff is reasonably new to me (although I looked at Java a few years ago). Anyway, I thought I would write a small class to begin with, with a...
11
by: Schizoid Man | last post by:
Hi, I have a function in which I am reading in an integer value, and dynamically creating an array of type double and size of the integer value: double compute(int steps, int typeopt) {...
2
by: robert.q.johnson | last post by:
Help. I am trying to create a web page in C# to display two rows of data with bar graphs (an image 1px). I get the max value of the row and use the following formula to size the height of the...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
22
by: j1mb0jay | last post by:
I have had to create a simple string encryption program for coursework, I have completed the task and now have to do a write up on how it could be improved at a later date. If you could look...
2
by: memo | last post by:
hi im in a trouble with c++ i made a project with c but icant convert c ++ here is the code with c #include<stdio.h> #include<stdlib.h> #include <conio.h>
10
by: lasmith329 | last post by:
I've never posted a question on any site before, but after racking my head over this hurdle for days, I've caved. I'm working on a program that creates a kml file and exports it to Google Earth. In...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.