[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Integer conversion error
From: |
Eric Volker |
Subject: |
Integer conversion error |
Date: |
Sun, 29 Apr 2001 02:45:40 GMT |
The following code generates some bizarre results:
#include <iostream.h>
float amount;
int cents;
int main()
{
cout << "Enter amount: ";
cin >> amount;
cents = int (amount*100);
cout << "$" << amount << " equals " << cents << " cents.\n";
return(0);
}
Sample output:
Enter amount: .76
$0.76 equals 75 cents.
Enter amount: .81
$0.81 equals 80 cents.
Enter amount: .31
$0.31 equals 30 cents.
Why is this happening? A float converted to an int should retain the
same value, not drop .01 as if it were rounding down. Please pardon my
ignorance if I'm missing something obvious.
- Integer conversion error,
Eric Volker <=