java double 2 decimal
String result = String.format("%.2f", value);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
Also in Java:
- array null pointer java
- @JsonSerializer spring specific field string to date
- create java windows application
- how to use for loop for array in java
- length of string java
- get current unix timestamp java
- how to create gravity in java
- store string elements in character array in java
- date to string java
- java nested loop
- java check if args is empty
- loop java
- Java int to roman
- cgange background from button click java fx
- protect java
- java "->"
- expression régulière seulement un espace java
- formatting an integer in java
- java create folder
- hashmap get value by key java
- char array to arraylist java
- select photo from camera android
- JavaFX onMouseEntered
- how to create a button in java