-1

I did all the due diligence I could and I found some answers in python, c++, php and JavaScript that didn't quite solve my problem...

In C# I am trying to round up the value of the 2nd decimal point to the nearest value that is divisible by 10.
I have a double value z = 14.066666
I need to turn z into 14.10

This is what I've done so far:

var result1 = Math.Ceiling(z);             // 15      (want 14.10)
var result2 = Math.Round(z,2);             // 14.7    (want 14.10)
var result3 = Math.Round(z,5);             // 14.0667 (want 14.10)
var result4 = Math.Ceiling(z*100)/100.0;   // 14.07   (want 14.10)
var result5 = Math.Ceiling(z*10)/10        // 14.1    (want 14.10)

Grateful for your consideration and thankful for any help that comes my way.

Radu Bartan
  • 483
  • 6
  • 12

1 Answers1

6

14.1 actually is equal to 14.10. The difference is in representation, not in value. To write this value with additional zero you have to specify proper formatting:

    double z = 14.066666;
    var result5 = Math.Ceiling(z*10)/10;
    Console.WriteLine("{0:N2}", result5);
Dialecticus
  • 16,400
  • 7
  • 43
  • 103