Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DOUBLE to LONG INT conversion

Status
Not open for further replies.

davidchardonnet

Programmer
Mar 21, 2001
167
FR
Hello,

I have a problem with Visual C++ compiler. The problem is in data conversion between LONG INT and DOUBLE. And the same occurs with INT and DOUBLE. Does anybody know how I can make it work? Here is the sample code:
-----------------------------------------------------------
double time_double_miliseconds;
double time_double_seconds;
long time_long_seconds;

...
// Here time_double_miliseconds contains 14.400000

time_double_seconds=(1000.0 * time_double_miliseconds);

// Here time_double_seconds contains 14400.00000

time_long_seconds = time_double_seconds;

// Here time_long_seconds contains 14399 !!!!!
-----------------------------------------------------------

What I want is to get 14400 into time_long_seconds. And I don't know why the compiler does this.

Thank you for your answers

David CHARDONNET
 
I tried to reproduce this using VC++ 6 SP5, but the result is 14400.


int main()
{
double time_double_miliseconds = 14.400000;
double time_double_seconds = 0.;
long time_long_seconds = 0L;

// Here time_double_miliseconds contains 14.400000
time_double_seconds =(1000.0 * time_double_miliseconds);

// Here time_double_seconds contains 14400.00000
time_long_seconds = time_double_seconds;

// prints 14400
std::cout << time_long_seconds << std::endl;
return 0;
}


Are you sure, the problem is not somewhere else in your code?

-- ralf

 
use static_cast
time_long_seconds = static_cast<long>(time_double_seconds); John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top