Rounding double values in C# -


I want a rounding method at double values ​​in C #. It should be able to round a double value for any round exact value. My code looks on the hand:

  Public Fixed Double Round (Double Number, Double Rounding Interface) {if (roundingInterval == 0.0) {return; } Double Intret = Math Abs (goring interval); Double Sign = Math. Sign (number); Double val = mathematics.bs (number); Double valIntvRatio = val / intv; Double K = Math.floor (valIntvRatio); Double M = valIntvRatio - k; Bool mGreaterThanMidPoint = ((M - 0.5)> = 1e-14)? right wrong; Bool mInmidpoint = (Math.Abus (m - 0.5) & lt; 1e-14)? right wrong; Return (mGreaterThanMidPoint || aminmadpoint)? Signature * ((K + 1) * Interv): Sign * (k * intv); Then the goal I (100, 3) 99 and the goal I (1.2345, 0.001) should be 1.235. The problem is, the goal I (1.275, 0.01) returns 1.27 instead of 1.28. The reason is that when double valIntvRatio = val / intv is executed, then double Valientities = 1.275 / 0.01, it gives 0.12749999999999. I know that this is a problem with double representation in any programming language. My question is, is there a standard code to work like this, need to worry about double accuracy? Here I have set tolerance to 1e-14, but for this problem it is very limited and I do not know what is the right tolerance. Thanks for any help.  

Example of using decimal , kibbis Told

  double D = 1.275; Mathematics.round (d, 2); // 1.27 monastery Round ((decimal) D, 2); // 1.28  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -