2

This line results in double value 3.33333333335

System.out.println("Average marks of " + name + " = " + (double)sum/3);

Is it possible to set a width of precision?

Ismail
  • 2,322
  • 1
  • 12
  • 26
drac_o
  • 427
  • 5
  • 11
  • 2
    Does this answer your question? [printf %f with only 2 numbers after the decimal point?](https://stackoverflow.com/questions/7197078/printf-f-with-only-2-numbers-after-the-decimal-point) – Amongalen Mar 13 '20 at 10:32
  • it's not a cast. It's a print – jhamon Mar 13 '20 at 10:32
  • 1
    No, the precision is fixed with double. That's why you need to format it if you want a different representation. – Kayaman Mar 13 '20 at 10:33
  • @Amongalen that's possible with printf method, but i was wondering if it is with print – drac_o Mar 13 '20 at 10:34
  • It's not. That's one reason `printf` exists, to allow for formatted output. – Kayaman Mar 13 '20 at 10:36

3 Answers3

2

You can use DecimalFormat or BigDecimal as follows:

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

public class Main {
    public static void main(String[] args) {
        int sum = 10;
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        System.out.println(Double.valueOf(decimalFormat.format((double) sum / 3)));

        // Another way
        System.out.println(new BigDecimal(String.valueOf((double) sum / 3)).setScale(2, RoundingMode.HALF_UP));

        // The better way using BigDecimal - thanks to @Andreas
        System.out.println(BigDecimal.valueOf(sum).divide(BigDecimal.valueOf(3), 2, RoundingMode.HALF_UP));
    }
}

Output:

3.33
3.33
3.33
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    Instead of potential precision loss when going through `double`, use `BigDecimal.valueOf(sum).divide(BigDecimal.valueOf(3), 2, RoundingMode.HALF_UP)`. You can now exceed the precision of a `double`, e.g. with 25 decimals, resulting in `3.3333333333333333333333333`, where going through `double` would result in `3.3333333333333335000000000`. – Andreas Mar 13 '20 at 11:02
0

Print the result of String.format(String, Object...) (which is the same as printf with an extra step), or use a BigDecimal (if you want a type with arbitrary precision).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

One solution is to write function:

static double roundWithTwoDigits(double x) {
    return (double) Math.round(x * 100) / 100;
}

Now you can use it:

System.out.println("Average marks of " + name + " = " + roundWithTwoDigits((double) sum / 7));
vbezhenar
  • 11,148
  • 9
  • 49
  • 63
  • I don't think it will work for really big numbers, will it? – Amongalen Mar 13 '20 at 12:16
  • Yep, for really big numbers one should use BigDecimals. It's more to answer the question as to how to get double-digit precise number without formatting. It's possible, but it's definitely limited and formatting should be used to format or BigDecimal should be used to control precision. – vbezhenar Mar 13 '20 at 12:42