c# - Rate of change with possible negative numbers -


i feel stupid i'm having problems calculating change in % when working negative numbers.

the calculation i'm using gives satisfying result when numbers > 0.

decimal rateofchange = (newnumber - oldnumber) / math.abs(oldnumber);

  1. lets have 2 numbers 0.476(newnumber) , -0.016(oldnumber) that's increase of 0.492 , my calculation rate of change 3 075%.

  2. if instead have 0.476(newnumber) , 0.001(oldnumber) that's increase of 0.475 , my calculation give me rate of change of 47 500% wich seems correct.

blue line represent example 1 , red line example two. in world blue line should have bigger % change.

how write calculation give me correct % change when dealing negative numbers? want handle both increases , decreases in % change.

i understand math issue , need work on math

blue line first example & red line second example

seems work me.

decimal newnumber = 0.476m; decimal oldnumber = -0.016m; decimal increase = newnumber - oldnumber; // 0.492 true decimal rateofchange = increase / math.abs(oldnumber); 

rateofchange equal approximately 30.75 3075% correct change.

the second example works too. increase -0.475 gives rateofchange equal -475 equal -47500% correct.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -