0

Hello im coding in C++ and i need some help with converting a double to an int. what a need is a way to get the first number from a double ie (3.5945) "3". and put that number into an int.

I'm using static_cast now and its returning a 0.

double X = 3.1234;
double Y = 4.3455;

int myIntX =  static_cast <int>(X);
int myIntY =  static_cast <int>(Y);

cout << myIntX << endl;
cout << myIntY << endl;

output....

0 0

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
Daniel Del Core
  • 3,071
  • 13
  • 38
  • 52
  • 4
    Please provide an example that reproduces your problem. This one doesn't: http://ideone.com/zOlRz – R. Martinho Fernandes Apr 04 '12 at 14:39
  • The code you have pasted does not have your bug. The bug must be in the code you *didn't* paste. Please produce a **short**, **complete** sample program that demonstrates the problem. See http://sscce.org – Robᵩ Apr 04 '12 at 14:40
  • my double number is actually this .... not sure what that means 3.89082e-316 my program is fairly large and the double and int variables are within that class. – Daniel Del Core Apr 04 '12 at 14:46
  • 4
    3.89082e-316 is 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000389082. Of course the integral part of that is 0. – R. Martinho Fernandes Apr 04 '12 at 14:49
  • ohk thanks alot i must be looking at the wrong place then.it must be when i calculate the double – Daniel Del Core Apr 04 '12 at 14:52

1 Answers1

0

Try this:

double x=3.1234;
int myintx=(int)x;

while(myintx%10!=0)
myintx/=10;

cout<<myintx;

This will give you the first digit your double as an int.

a_123
  • 183
  • 2
  • 13