-2

Code

package Java.School.IX;
import java.util.*;

public class Invest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter principle - Rs ");
        double p = sc.nextDouble();
        //Interest for 1st yr - 
        double i1 = (p*3*5)/100;
        double a = Math.round(i1,2);
        System.out.println("Interest for 1st year is Rs " + a);
        //Interest for 2nd yr - 
        double p1 = p + i1;
        double i2 = (p1*3*5)/100; 
        System.out.println("Interest for 2nd year is Rs "+ i2); 
        sc.close();
    }
}

Issue

I tried using Math.round(double, noOfPlaces), but this code doesn't seem to be working. I need some guidance.

Pls help me to round of the decimal to 2 decimal places. How to fix this?

  • 2
    Please explain the output you got, and the output you expected. – hc_dev May 28 '22 at 18:02
  • Also read [ask] and do required research here on SO, e.g. search for `[java] round decimal 2 digits`. – hc_dev May 28 '22 at 18:04
  • Many a times, I get the interest as 18.593958...... (a number similar to this with long decimal numbers). So i wanna round it off to 2 decimal places. Like rounding of 18.34867 to just 18.35 – Challenged May 28 '22 at 18:06
  • 4
    By the way, for money matters you would be using `BigDecimal` rather than `double`. Floating-point types [trade away accuracy](https://en.m.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems) in exchange for faster performance. – Basil Bourque May 28 '22 at 18:08
  • 1) Where did you find `Math.round (double x, int p)`? I know of `Math.round` that take 1 argument in `java.lang.Math`, but I don't know of a standard one that takes 2 arguments. 2) `Math.round (double x)` returns a `long` and `Math.round (float x)` returns an `int`. 3) `float` and `double` are not suitable for precise representations of decimal fractions. `double penny =0.01` won't be exactly 0.01. Try converting 0.01 to binary and see what happens. – Old Dog Programmer May 28 '22 at 18:39
  • Our teacher had showed to us in BlueJ. But turns out it is not working in VS Code and my teacher is not gonna say it again – Challenged May 29 '22 at 05:41
  • @Challenged do you have documentation for `Math.round(double, noOfPlaces)` that you can share with us? – Old Dog Programmer May 30 '22 at 02:03
  • Sorry to say I don't have any docs for the specified function – Challenged May 30 '22 at 10:44

4 Answers4

0

Try using the NumberFormat class. It can format the number with exact number of digits past decimal point.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • This could also been a comment with proper link. Please give a short code example to illustrate application to this question. – hc_dev May 28 '22 at 18:09
0

Don't round the actual value. Let System.out.printf() do the work for you.

double [] data = {1.4452, 123.223,23.229};
for (double v : data) {
   System.out.printf("%.2f%n",v);
}

prints

1.45
123.22
23.23
WJS
  • 36,363
  • 4
  • 24
  • 39
  • Man why have you written "%.2f%n" ? What does it mean (I am a beginner in java. So asking its meaning) – Challenged May 28 '22 at 18:19
  • That's why I posted a linke to the method. It says for %b.cf" allow b places to the left of the decimal and c to to the right. It rounds half up. if b or c are omitted, it allows any precision in that place. – WJS May 28 '22 at 18:28
0

There are multiple ways to do this.

Math.round()

double i = 2.3333;
System.out.println(Math.round(i*100) / 100);

NumberFormat.format()

NumberFormat formatter = new DecimalFormat("#0.00");     
System.out.println(formatter.format(2.3333));

System.out.printf()

System.out.printf("%.2f", 3.2222);

Probaly there are 10 more ways of doing it, thats just what I can think of right now.

Laisender
  • 714
  • 1
  • 5
  • 10
0

In favor for Basil's comment:

  • prefer BigDecimal for currency amounts
// I = PRT
// Returns: interest for n-th year for principal amount (also printed on stdout)
BigDecimal interestFor(BigDecimal principalAmount, int year) {
    BigDecimal interest = principalAmount.multiply(3*5).divide(100); // FIX:  check formula here to be correct on years!

    // Locale.US for $, but if we want Indian Rupee as: Rs
    Locale locale = new Locale("en", "IN");
    String interestFormatted = NumberFormat.getCurrencyInstance(locale).format(interest);
    
    System.out.printf("Interest for year %d is %s\n", year, interestFormatted);
    
    return interest;
}

For the placeholder literals %s (string formatted), %d (decimal formatted), see Java's Formatter syntax.

See also:

hc_dev
  • 8,389
  • 1
  • 26
  • 38