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

Double To Int -- Coversions

compman9902
105 100+
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.
Jul 23 '07 #1
5 1849
ilikepython
844 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. double num = 88.92;
  2. int x = num;                   // gives a warning
  3. cout << x << endl;
  4. // or...
  5. double num = 88.92;
  6. int x = int(num);      // no warning
  7. cout << x << endl;
  8.  
Jul 23 '07 #2
compman9902
105 100+
That's pretty simple:
Expand|Select|Wrap|Line Numbers
  1. double num = 88.92;
  2. int x = num;                   // gives a warning
  3. cout << x << endl;
  4. // or...
  5. double num = 88.92;
  6. int x = int(num);      // no warning
  7. cout << x << endl;
  8.  
Wow, thank you for getting back to my post so quickly.
It worked perfectly.
Jul 23 '07 #3
ilikepython
844 Expert 512MB
Wow, thank you for getting back to my post so quickly.
It worked perfectly.
Your welcome .
Jul 23 '07 #4
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).
Jul 23 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. double d = 88.62;
  2. stringstream ss;
  3. ss << d;
  4. int result;
  5. ss >> result;               //the 88
  6.  
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.
Jul 23 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
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...
20
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; ...
31
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...
10
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...
10
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);
3
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...
67
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...
1
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(); ...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...
0
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,...

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.