[ Moved thread ]
Printable View
[ Moved thread ]
Just to clarify, what I want to do is this: Give are set of float values essentially with 2 decimal places. The goal is eliminate the decimal point and get the integer value, which means simply multiply be 100.Quote:
Originally Posted by kyoryu
12.34 -> 1234
Now I am going to eliminate float i think and store th original value in doubles.
On second thought:
It would be less complicated to do it this way:Quote:
First solution (rely on rounding):
(int)(.11 * 100 + .005) should always produce 11
(int)(.9 * 10 + .05) should always produce 9
(int)(.359 * 1000 + .0005) should always produce 359
(int)(.11 * 100 + .5)
(int)(.9 * 10 + .5)
(int)(.359 * 1000 + .5)
How are you reading the information in in the first place? Given that you always want to store the information as an int, it may be easier to never actually use a float.Quote:
Originally Posted by zspirit
How reasonable this is is dependent on how you're getting the data.
Though, whether you store as float or double, you'll be better off if you always use the same data type for all caclulations, and then convert to int at the end of it all.
See the code I posted for an example of this.
Here is the solution!!!
From the original post:
The trick is to typecast it first to float and than to short int!!! ahhhCode:// case 2
float fVal = .11;
short int test = (short int) (float) (fVal * 100); // now this right!
if we don't typecast to float, the result is treated as double and with high precision its value becomes .1099999, multiply this by 100 is 10.9, typecast this to int gives 10
typecasting 10.9 to int would simply discard the .9 part but typecasting it to float first will still work with .9 part because it is float and its value is read as 11.
:D
It don't workQuote:
Originally Posted by zspirit
try declaring fVal like double directly
Sorry, my english is not good