0

Floating point with more than a zero after the point is not showing up to two decimal point.

I've tried DecimalFormat to convert. It's worked for round up except(with more than a zero) ex- 25.00000

    DecimalFormat df = new DecimalFormat("0.00");
    df.setMinimumFractionDigits(2);
    Double.parseDouble(df.format(frog_per));

I expect the output of 25.00000 to be 25.00, but the actual output is 25.0.

  • 7
    Where do you output anything? You use `Double.parseDouble` on the result, which turns it into a `double` again – f1sh Oct 14 '19 at 08:05
  • Like @f1sh said, you should directly print this: `df.format(frog_per)`. – Mushif Ali Nawaz Oct 14 '19 at 08:10
  • 1
    @MushifAliNawaz yapp,,got it. – sohanbappy Oct 14 '19 at 08:12
  • Double and float are without any inherent precision (hence _floating_ point). BigDecimal is a _fixed poiint_ class. `new BigDecimal("0.00")` would give a zero with two decimals. – Joop Eggen Oct 14 '19 at 08:13
  • System.out.println(new DecimalFormat("#.00").format(25.0)); simply works. Besides, be aware what @Joop eggen says. – Michal Oct 14 '19 at 08:58
  • 1
    Possible duplicate of [How to round a number to n decimal places in Java](https://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) – bvdb Oct 14 '19 at 09:13
  • @sohanbappy - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 29 '20 at 19:59

4 Answers4

2

You can use String.format(String format, Object... args)

class Main {
    public static void main(String[] args) {
        System.out.println(String.format("%.2f",25.0000));
    }
}

Output:

25.00
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • @sohanbappy - do not do this as long as you do not understand the consequences. – Michal Oct 14 '19 at 08:48
  • I would NOT recommend using `String.format` to format information to be DISPLAYED. The reason ist that the `DecimalFormat` and the other formatting classes can handle a number of localisation aspects that String.format does NOT. The usage of String.format is therefore only for information which will NEVER be shown in language other then English. – Michal Oct 14 '19 at 08:53
  • But that is not what your answer contains. – Michal Oct 14 '19 at 09:05
  • The point is, IMHO it is better to point the OP to correct answer to his question - how to use DecimalFormat correctly - then provide - I'm sorry for that expresion - misleading answers like 'use `String.format(String format, Object... args)` for display purpose. – Michal Oct 14 '19 at 09:10
  • As I say, I consider the answer misleading. Therefore I also downvoted. – Michal Oct 14 '19 at 09:11
  • Your answer would be ok without the word 'display'. – Michal Oct 14 '19 at 09:13
  • 2
    there is also a `printf` method which does the formatting and printing together. – bvdb Oct 14 '19 at 09:14
0

https://onlinegdb.com/BJ5W0ibKB

Kindly change DecimalFormat df = new DecimalFormat("0.00"); to DecimalFormat df = new DecimalFormat("#,###,##0.00");

vishu9219
  • 761
  • 6
  • 14
0

Your format pattern 0.00 is fine.
Just don't construct the double out of the formatted string in order to display it as a formatted output.

Double object created from input 25.00 or 25.00000 or 25 is having the value of 25. It stores the value and never the formatting.

If you want this object to be used to produce 25.00 output you have to format it again, by using either method used in other answers:

Double d = 25.00000d;
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(d));
System.out.printf("%.2f", d);

produces expected output:

25.00
25.00
diginoise
  • 7,352
  • 2
  • 31
  • 39
-1

This is how u round off 2 decimals.

DecimalFormat df2 = new DecimalFormat(".##");
df2.format(variable)
bvdb
  • 22,839
  • 10
  • 110
  • 123
  • `#` denotes optional decimal digits, so `25.00000` formatted using `.##` format will display `25` - please test – diginoise Oct 14 '19 at 09:13