java string format .2f
String.format("%.2f", 3.45);
how to limit double decimal places java
package com.mkyong;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalExample {
private static DecimalFormat df2 = new DecimalFormat("#.##");
public static void main(String[] args) {
double input = 3.14159265359;
System.out.println("double : " + input);
System.out.println("double : " + df2.format(input)); //3.14
// DecimalFormat, default is RoundingMode.HALF_EVEN
df2.setRoundingMode(RoundingMode.DOWN);
System.out.println("\ndouble : " + df2.format(input)); //3.14
df2.setRoundingMode(RoundingMode.UP);
System.out.println("double : " + df2.format(input)); //3.15
}
}
Copy
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us