-1

I have decimal numbers. I want to round up them with two places:

Original Value : 6.3619 
What I want    : 6.37

Original Value : 5.12003
What I want    : 5.13

I tried Math.Celing, Math.Round. But these methods don't give me the values I want.

I don't want to truncate decimal value.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
emert117
  • 1,268
  • 2
  • 20
  • 38

3 Answers3

3

Try Ceiling method:

  1. Scale the value up: 6.3619 -> 636.19
  2. Truncate with a help of Math.Ceiling: 636.19 -> 637
  3. Finally, scale the result down: 637 -> 6.37

Code:

 var result = Math.Ceiling(value * 100.0) / 100.0;

Demo:

double[] tests = new double[] {
  6.3619,
  5.12003,
};

string report = string.Join(Environment.NewLine, tests
  .Select(test => $"{test,10} -> {Math.Ceiling(test * 100) / 100.0}"));

Console.Write(report);

Outcome:

  6.3619 -> 6.37
 5.12003 -> 5.13
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You should try this.

 var answer = Math.Ceiling(yourvalue * 100) / 100;

Please mark it as answer if this helps.

0

Try using this

private double RoundValueUp(double value, int NbDecimals)
{
    double multiplier = Math.Pow(10, Convert.ToDouble(NbDecimals));
    return Math.Ceiling(value * multiplier) / multiplier;
}

Thus you can use it like this RoundValueUp(6.3619, 2); In reference to LINK

AhmadMM
  • 371
  • 4
  • 16