yazz.com
yazz.com

Reputation: 58786

How to cut off decimal in Java WITHOUT rounding?

I have a series of Java decimals like:

0.43678436287643872
0.4323424556455654
0.6575643254344554

I wish to cut off everything after 5 decimal places. How is this possible?

Upvotes: 10

Views: 57969

Answers (7)

Peter Lawrey
Peter Lawrey

Reputation: 533500

If you want to keep things fast and simple. ;)

public static void main(String... args) {
    double[] values = {0.43678436287643872, 0.4323424556455654, 0.6575643254344554,
            -0.43678436287643872, -0.4323424556455654, -0.6575643254344554,
            -0.6575699999999999 };

    for (double v : values) 
        System.out.println(v + " => "+roundDown5(v));
}

public static double roundDown5(double d) {
    return ((long)(d * 1e5)) / 1e5;
    //Long typecast will remove the decimals
}

// Or this. Slightly slower, but faster than creating objects. ;)
public static double roundDown5(double d) {
    return Math.floor(d * 1e5) / 1e5;
}

prints

0.43678436287643874 => 0.43678
0.4323424556455654 => 0.43234
0.6575643254344554 => 0.65756
-0.43678436287643874 => -0.43678
-0.4323424556455654 => -0.43234
-0.6575643254344554 => -0.65756
-0.6575699999999999 => -0.65756

Upvotes: 32

FrankMonza
FrankMonza

Reputation: 2044

To generalize Peter answer you can do:

public static double round(double n, int decimals) {
    return Math.floor(n * Math.pow(10, decimals)) / Math.pow(10, decimals);
}

Upvotes: 1

Kris
Kris

Reputation: 8868

Double.parseDouble(String.valueOf(x).substring(0,7));

OR

Double.valueOf(String.valueOf(x).substring(0,7));

where x contains the value you want to cut such as 0.43678436287643872

Upvotes: 11

nowaq
nowaq

Reputation: 2748

I would do it with regular expressions like this:

    double[] values = { 
            0.43678436287643872,
            0.4323424556455654,
            0.6575643254344554,
            -0.43678436287643872,
            -0.4323424556455654,
            -0.6575643254344554
    };

    Pattern p = Pattern.compile("^(-?[0-9]+[\\.\\,][0-9]{1,5})?[0-9]*$");
    for(double number : values) {
        Matcher m = p.matcher(String.valueOf(number));
        boolean matchFound = m.find();
        if (matchFound) {
            System.out.println(Double.valueOf(m.group(1)));
        }
    }

The pattern can be easily modified if you need to support more/less decimal places.

Upvotes: 1

Costis Aivalis
Costis Aivalis

Reputation: 13728

The DecimalFormat could also be of assistance here:

    double d = 0.436789436287643872;
    DecimalFormat df = new DecimalFormat("0.#####");
    df.setRoundingMode(RoundingMode.DOWN);

    double outputNum = Double.valueOf(df.format(d));
    String outpoutString = df.format(d);

Upvotes: 12

Artem
Artem

Reputation: 4397

float f = 0.43678436287643872;
BigDecimal fd = new BigDecimal(f);
BigDecimal cutted = fd.setScale(5, RoundingMode.DOWN);
f = cutted.floatValue();

Upvotes: 19

thejartender
thejartender

Reputation: 9379

I believe the java.text.DecimalFormat class is what you need.

Upvotes: 2

Related Questions