What happened here????? Wrong variable type???
-
Here is the codes float a,b; a = (( (AlphaTotal - PrevAlphaTotal) / PrevAlphaTotal ) * 100 ); b = (( (BetaTotal - PrevBetaTotal ) / PrevBetaTotal ) * 100 ); CString testing; CStringList ReturnedData; testing.Format("Alpha Percentage %f", a ); ReturnedData.AddTail(testing); testing.Format("Beta Percentage %f", b ); ReturnedData.AddTail(testing); (--->ReturnedData will be used to output in view class) The results would be: If a or b = 65.50...it displays 0.00 If a or b = 180.50...it displays 100.00 Why i know that because.....I try just type some number in the formula...for example, a = ((700-500)/500)*100; b = ((81-60)/60)*100; Then output is: Alpha Percentage 0.00 Beta Percentage 0.00 How to fix it?????????????????????? ;P
-
Here is the codes float a,b; a = (( (AlphaTotal - PrevAlphaTotal) / PrevAlphaTotal ) * 100 ); b = (( (BetaTotal - PrevBetaTotal ) / PrevBetaTotal ) * 100 ); CString testing; CStringList ReturnedData; testing.Format("Alpha Percentage %f", a ); ReturnedData.AddTail(testing); testing.Format("Beta Percentage %f", b ); ReturnedData.AddTail(testing); (--->ReturnedData will be used to output in view class) The results would be: If a or b = 65.50...it displays 0.00 If a or b = 180.50...it displays 100.00 Why i know that because.....I try just type some number in the formula...for example, a = ((700-500)/500)*100; b = ((81-60)/60)*100; Then output is: Alpha Percentage 0.00 Beta Percentage 0.00 How to fix it?????????????????????? ;P
Are AlphaTotal, PrevAlphaTotal, etc. all integral types? Probably so. Use floating point types (or cast the variables to doubles) otherwise the division operator does an integer divide, which throws away any remainder. That's why 200/500*1000 is 0 - 200/500 is an integer divide and returns 0. --Mike-- http://home.inreach.com/mdunn/ "Holding the away team at bay with a non-functioning phaser was an act of unmitigated gall. I admire gall." Lt. Cmdr. Worf
-
Are AlphaTotal, PrevAlphaTotal, etc. all integral types? Probably so. Use floating point types (or cast the variables to doubles) otherwise the division operator does an integer divide, which throws away any remainder. That's why 200/500*1000 is 0 - 200/500 is an integer divide and returns 0. --Mike-- http://home.inreach.com/mdunn/ "Holding the away team at bay with a non-functioning phaser was an act of unmitigated gall. I admire gall." Lt. Cmdr. Worf
Oh thanks!!!! It works now... but i have one more question. For example, a = 180.98763 I just want to display 180.99....and save it to a char or CString because i want to save to my CStringList........what can i do??? can i use: char Buffer[128]; sprintf(Buffer, " %f %f", a,b); I have a and b......I want to display liked: | 180.98 | 123.56 | ??????
-
Oh thanks!!!! It works now... but i have one more question. For example, a = 180.98763 I just want to display 180.99....and save it to a char or CString because i want to save to my CStringList........what can i do??? can i use: char Buffer[128]; sprintf(Buffer, " %f %f", a,b); I have a and b......I want to display liked: | 180.98 | 123.56 | ??????
When i use this code to save the result to a char, char Buffer[128]; sprintf(Buffer, " %f %f", a,b); my program is forced to terminate....why?
-
Oh thanks!!!! It works now... but i have one more question. For example, a = 180.98763 I just want to display 180.99....and save it to a char or CString because i want to save to my CStringList........what can i do??? can i use: char Buffer[128]; sprintf(Buffer, " %f %f", a,b); I have a and b......I want to display liked: | 180.98 | 123.56 | ??????
For example, a = 180.98763 I just want to display 180.99 A format string of "%.2f" will do that. Check the printf() documentation for a full list of the format strings. --Mike-- http://home.inreach.com/mdunn/ "Holding the away team at bay with a non-functioning phaser was an act of unmitigated gall. I admire gall." -- Lt. Cmdr. Worf