First of all, thank you for taking the time to look at my post.
My question is pretty plain: "How do I convert a double variable into an int variable?"
For example, say I have a double variable and its value is 88.92.
Then I have an empty int variable.
How would I make that int variable equal 88?
Would that same method work if the double variable equaled just 56?
Well, thank you for taking a look at my question(s).
Also, please include code with your post.
Again, Thank You.
5 1865
First of all, thank you for taking the time to look at my post.
My question is pretty plain: "How do I convert a double variable into an int variable?"
For example, say I have a double variable and its value is 88.92.
Then I have an empty int variable.
How would I make that int variable equal 88?
Would that same method work if the double variable equaled just 56?
Well, thank you for taking a look at my question(s).
Also, please include code with your post.
Again, Thank You.
That's pretty simple: -
double num = 88.92;
-
int x = num; // gives a warning
-
cout << x << endl;
-
// or...
-
double num = 88.92;
-
int x = int(num); // no warning
-
cout << x << endl;
-
That's pretty simple: -
double num = 88.92;
-
int x = num; // gives a warning
-
cout << x << endl;
-
// or...
-
double num = 88.92;
-
int x = int(num); // no warning
-
cout << x << endl;
-
Wow, thank you for getting back to my post so quickly.
It worked perfectly.
Wow, thank you for getting back to my post so quickly.
It worked perfectly.
Your welcome .
Be careful with double to int conversions. ints have a smaller max size then doubles so if the value is too high you will get some bad behaviour. In general casting like this is bad (although int to double is fine).
Be careful with double to int conversions. ints have a smaller max size then doubles so if the value is too high you will get some bad behaviour. In general casting like this is bad (although int to double is fine).
Pay attention to what Benny says.
You cannot convert a double to an int. Int has no decimal places. If you cast, you force a truncation of the decimal portion.
You can avoid the cast by a) calling a function that you write that has a double argument and returns an int, pr b) use a stringstream. -
double d = 88.62;
-
stringstream ss;
-
ss << d;
-
int result;
-
ss >> result; //the 88
-
Here the decimal point and the decimal placesare still in the stringstream. You culd have this logic inside a function such that after you get the 88 youcould skip over the decimal point and retrieve the decimal places as a second int sop you could determine whether the result should be 88.62 or 88.63.
As a rule, do no mix integers and floating point. Floating point is generally for scientific use where extreme accuracy is not required.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Sydex |
last post by:
When I compile code I get error
C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double
(double)' to 'double (__cdecl *)(double)'
in this part :
double Integration::quad2d(double (*func)(double,double))
{
nfunc = func ;
return qgaus(f1,x1,x2);//error there
|
by: Anonymous |
last post by:
Is there a non-brute force method of doing this?
transform() looked likely but had no predefined function object.
std::vector<double> src;
std::vector<int> dest;
std::vector<double>::size_type size = src.size();
dest.reserve(size);
for (std::vector<int>::size_type i = 0;
|
by: Bjørn Augestad |
last post by:
Below is a program which converts a double to an integer in two
different ways, giving me two different values for the int. The basic
expression is 1.0 / (1.0 * 365.0) which should be 365, but one variable
becomes 364 and the other one becomes 365.
Does anyone have any insight to what the problem is?
Thanks in advance.
Bjørn
|
by: Robert Palma |
last post by:
I'm having trouble figuring out how to pass a
pointer to a double array (1 dimensional)
to a C function.
Declaring array as: double xx;
Declaring func. int process( double *input )
Calling func. as one of the following:
|
by: Bryan Parkoff |
last post by:
The guideline says to use %f in printf() function using the keyword
float and double.
For example
float a = 1.2345;
double b = 5.166666667;
printf("%.2f\n %f\n", a, b);
| |
by: BlueTrin |
last post by:
I am using a DLL written in C, it uses some pointers on functions, I
have defined a wrapper around it in C# which uses some delegates:
#region Delegates and Marshalling to call solvopt
public delegate double funCallback(double x);
public delegate double funcCallback(double x);
public delegate void gradCallback(double x, double v);
public delegate void gradcCallback(double x, double v);
|
by: lcw1964 |
last post by:
This may be in the category of bush-league rudimentary, but I am quite
perplexed on this and diligent Googling has not provided me with a
clear straight answer--perhaps I don't know how to ask the quesion.
I have begun to familiarize myself here with the gcc compiler in a
win32 environment, in the form of MinGW using both Dev C++ and MSYS as
interfaces.
I have recompiled some old math code that uses long double types
throughout and...
|
by: JWest46088 |
last post by:
I keep getting these error messages:
area(double,double) in Rectangle cannot be applied to ()
return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ^
perimeter(double,double) in Rectangle cannot be applied to ()
return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter();
^
setSides(double,double) in Rectangle cannot be applied to (double)...
|
by: Genro |
last post by:
#include<stdio.h>
#include<TX/graphics.h>
#include<time.h>
// I need help!
struct Krug{
double _x;
double _y;
double _skox;
double _skoy;
double _granx1;
|
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...
|
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,...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |