Change double decimal spaces depending on int value

Viewed 35

i want to change the decimal spaces of an double, depending on the int value that i use. I Figured out, that i can change the decimal spaces "manually" by changing to 10 or 1000 and so on, but i dont know how to accomplish this, other than changing said values in the code.

My second quest is how to round double values for example 19.657 up to 19.66, or 19.652 down to 19.65 .

Note :

  • WITHOUT the use of imports and only the default package

  • I use Java with Eclipse as my IDE

Thank you in advance for your effort :)

    double x = 19.657821456;
    int y = 3;  

        x = x*10; 
        x = (double)((int) x);
        x = x /10;
                 
            
3 Answers

You can use the DecimalFormat class to format your doubles.

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

This will also round values if they exceed the amount of decimals you specified

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

You can use Decimal Formate to format double value

Example:

private static final DecimalFormat df = new DecimalFormat("0.00");

public static void main(String[] args) {

    double input = 3.14159265359;

    System.out.println("double : " + input);
    System.out.println("double : " + df.format(input));    //3.14

    // DecimalFormat, default is RoundingMode.HALF_EVEN
    df.setRoundingMode(RoundingMode.DOWN);
    System.out.println("\ndouble (RoundingMode.DOWN) : " + df.format(input));  //3.14

    df.setRoundingMode(RoundingMode.UP);
    System.out.println("double (RoundingMode.UP)  : " + df.format(input));    //3.15

}

Output :

double : 3.14159265359
double : 3.14

double (RoundingMode.DOWN) : 3.14
double (RoundingMode.UP)  : 3.15

Variable number of digits after decimal. Basically same idea as code in question, just using Math methods, calculating the multiplier as needed (10 or 1000 in question):

public static double round(double value, int digits) {
    if (digits < 0) 
        throw new IllegalArgumentException("digits must be non-negative: " + digits);

    var mult = Math.pow(10, digits);
    return Math.rint(value * mult) / mult;
}

If not allowed to use Math (standard lib, no import needed), we can calculate the multiplier in a loop and use an offset of 0.5 to round to the nearest integer (negative for negative numbers):

public static double round(double value, int digits) {
    if (digits < 0) 
        throw new IllegalArgumentException("digits must be non-negative: " + digits);

    var mult = 1.0;
    for (var i = 0; i < digits; i++) {
        mult *= 10;
    }
   
    var offset = value>0 ? 0.5 : -0.5;
    return (int) (value * mult + offset) / mult;
}

Be warned that these methods will only work for small number of digits (intermediate results eventually exceeding the integer or even the double value range)
not tested - please test code before using

Related