473,788 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8261
"da Vinci" <bl***@blank.co m> wrote in message
news:cf******** *************** *********@4ax.c om...
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(v alue, &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.co m> 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.co m> 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
1986
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 found it the other day and decided to fix it up. I have one problem that I can't figure out in it still. When I run the program it prompts for the option number as it is supposed to, but when I enter in the number it outputs that I have entered an...
18
3356
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 some problems getting it to work and we don't know why. Basically the function is accepting the parameters, and then returning an error and never performing the update.
36
2115
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 property and 2 methods, along with a constructor (where you use New(), I think, yes?). So, I decided to create a class that represents a die, which can be rolled - using a Roll() method - and which has a property to find out the value on the face of...
11
5489
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) { double *price_array; price_array = (double *) malloc(sizeof(int) * steps);
2
1705
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 image, but the bar graphs are not sizing correctly. Can anyone provide me with a solution or any ideas. rowValue / maxRowValue * maxBarHeight
116
35999
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 problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
22
7698
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 through the code and point me in the correct direction one would be very grateful. Example Input : j1mb0jay Example Output 1 : rZHKZbYZWn/4UgL9mAjN2DUz7X/UpcpRxXM9SO1QkvkOe5nOPEKnZldpsB7uHUNZ Example Output 2 :...
2
1530
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
15918
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 that file, I want to create a 5 mile radius circle around the placemark. To do that, I worked with the kml circlegen source I found online in php. It calculates the latitudes and longitudes of the points that make up the circle. The calculation...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.