Solved Double to 2 decimals

Discussion in 'Plugin Development' started by Kassestral, Feb 15, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Kassestral

    When I format my numbers it takes away the last '0' from the digit
    example=>[ 80.80 would return 80.8 ]
    but my code should make it display correctly, but it doesn't.

    Any help?

    My method (non-working)
    Code:
        public static double roundTwoDecimals(double amount) {
                DecimalFormat twoForm = new DecimalFormat("##.00");
                return Double.valueOf(twoForm.format(amount)).doubleValue();
              }
     
  2. Offline

    teej107

    @Kassestral You don't need to box the double like that.
    Code:
    DecimalFormat twoForm = new DecimalFormat("##.00");
    System.out.println(twoForm.format(80.80));
    This code printed out the inputted number just fine!
     
  3. I think an easy way would be
    Code:
    Double double = 100.1235325
    Math.round(double, 2);
    
    that would return 100.12
     
  4. Offline

    Kassestral

    @sn1cko
    "Math.round(double, 2);" Does not take two parameters. And it only rounds to whole numbers
     
  5. Offline

    mythbusterma

    Well, you thought wrong.

    @Kassestral

    Why don't you listen to Teej? His is the only correct answer in this thread.
     
  6. Offline

    Kassestral

    @mythbusterma @teej107
    I need it to return a double, not a string, and doing Parsing a double from the string will still return 80.8
     
  7. Offline

    mythbusterma

    @Kassestral

    I presume you didn't pass pre-algebra then, as 80.8 and 80.80 are the same number, the only reason you would remove trailing zeros is if you wanted to format the number for output.
     
  8. Offline

    Kassestral

    @mythbusterma
    I know it's the same number, your attempt at insulting someone that you are meant to be helping is childish.

    However what I was trying to do I have fixed by myself
     
  9. sorry for my wrong post, here is a working method
    Code:
    DecimalFormat df = new DecimalFormat("#.00");
    df.format(1.1235463);
    
     
  10. Offline

    Kassestral

    @sn1cko
    I have already sorted it, but thanks anyway
     
Thread Status:
Not open for further replies.

Share This Page