Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round off a double while maintaining the trailing zero

Here is my function to roundoff a number upto two decimals but when the rounded off number is 1.50 it seems to ignore the trailing zero and just returns 1.5

public static double roundOff(double number) {
        double accuracy = 20;
        number = number * accuracy;
        number = Math.ceil(number);
        number = number / accuracy;
        return number;
    }

So if I send 1.499 it returns 1.5 where as I want 1.50

like image 629
Nick Div Avatar asked Jan 08 '15 02:01

Nick Div


1 Answers

This is a printing poblem:

double d = 1.5;
System.out.println(String.format("%.2f", d)); // 1.50
like image 141
Jean Logeart Avatar answered Sep 25 '22 07:09

Jean Logeart