0

I want my code to show only 2 digits after the decimal, I will show you my code for calculations

public class CalculatorModel
{
  private double sbdValue;

  public void addTwoNumbers(double multiplier, double nbd, double sbd)
  {
    sbdValue = nbd * 1.25;
  }

  public double sbdValue()
  {
    return sbdValue;
  }
}
pzp
  • 6,249
  • 1
  • 26
  • 38

1 Answers1

0

You may use one on these options, based on your language:

Java:

public double sbdValue()
{
   return Math.round(sbdValue * 100.0) / 100.0
}

C#

public double sbdValue()
{
   return Math.Round(sbdValue, 2);
}
nramirez
  • 5,230
  • 2
  • 23
  • 39
  • Thank you so much, but this is rounding 114.9956 to be 115, is there another way to keep it 114.99 ? – Aboelmagd Saad Aboelmagd May 01 '15 at 23:43
  • Sorry I just fixed it, I just missed something Math.Round(sbdValue, 2); I assumed you're using c#, aren't you? @AboelmagdSaadAboelmagd – nramirez May 01 '15 at 23:52
  • I'm sorry, I don't understand what is C# , i'm very new to java world. The code you wrote with 2 also didn't work unfortunately :( – Aboelmagd Saad Aboelmagd May 02 '15 at 20:04
  • C# is another programming language (http://en.wikipedia.org/wiki/C_Sharp_(programming_language)). Try the solution in Java, It works, I think you're doing something wrong, but you can also try these alternatives http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – nramirez May 05 '15 at 01:29