c# - When should I use integer for arithmetic operations instead of float/double -


some user said me should not use float/double stuff student grades, see last comments: sequenceequal() not equal custom class , float values

"because easier , safer arithmetic in integers, , convert suitable display format @ last minute, attempt arithmetic in floating point formats. "

i tried said result not satisfying.

   int grade1 = 580;    int grade2 = 210;    var average = (grade1 + grade2) / 2;           string result = string.format("{0:0.0}", average / 100); 

result "3,0"

    double grade3 = 5.80d;     double grade4 = 2.10d;     double average1 = (grade3 + grade4) / 2;     double averagefinal = math.round(average1);     string result1 = string.format("{0:0.0}", averagefinal); 

result1 "4,0"

i expect 4,0 because 3,95 should result in 4,0. worked because use math.round again works on double or decimal. not work on integer.

so wrong here?

you need "convert suitable display format @ last minute":

string result = string.format("{0:0.0}", average / 100.0); 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -