0

I can't understand where's the problem and why my number does not become with 2 numbers in the end. Tried various examples, but still receiving an problem.

public double apskaiciuotiKainaUzLitra(int kaina, int talpa) {
        double kainaLitro = (1000 / talpa) * kaina;
        double roundOff = Math.round(kainaLitro * 100.0) / 100.0;
        return roundOff;
    }
HenrikasB
  • 321
  • 1
  • 7

2 Answers2

1

Use String.format() to format

public double apskaiciuotiKainaUzLitra(int kaina, int talpa) {
        double kainaLitro = (1000 / talpa) * kaina;
        double roundOff = Math.round(kainaLitro * 100.0) / 100.0;
        return new Double(String.format( "%.2f", roundOff));
}
Sudhir Ojha
  • 3,009
  • 2
  • 11
  • 23
0
NumberFormat formatter = new DecimalFormat("#0.00");

After doing that, you can use formatter.

Onur Arı
  • 502
  • 3
  • 16