Sairam Sattar
Sairam Sattar

Reputation: 35

Issue in Decimal Points

I am facing an issue in decimal point I want to get only two decimal points only so I used this below code

 public class TestDateExample1 {
  public static void main(String[] argv){
      double d = 2.34568;
         DecimalFormat f = new DecimalFormat("##.00");
         System.out.println(f.format(d));
    }
  }

when I run this code the output is 2.35 but I dont need any increment when the decimal point is higher than "5" I need exact decimal point like 2.34.

Upvotes: 1

Views: 65

Answers (2)

user2862544
user2862544

Reputation: 425

DecimalFormat.setRoundingMode() needs to be set to RoundingMode.FLOOR

Upvotes: 2

Mureinik
Mureinik

Reputation: 311163

You could use DecimalFormat#setRoundingMode to control how the rounding is done, and specify you always want to round down (truncate):

DecimalFormat f = new DecimalFormat("##.00");
f.setRoundingMode(RoundingMode.DOWN);
System.out.println(f.format(d));

Upvotes: 3

Related Questions