java round double to 2 decimal places
Java
String result = String.format("%.2f", value);import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalExample {
private static DecimalFormat df = new DecimalFormat("0.00");
public static void main(String[] args) {
double input = 1205.6358;
System.out.println("salary : " + input);
// DecimalFormat, default is RoundingMode.HALF_EVEN
System.out.println("salary : " + df.format(input)); //1205.64
df.setRoundingMode(RoundingMode.DOWN);
System.out.println("salary : " + df.format(input)); //1205.63
df.setRoundingMode(RoundingMode.UP);
System.out.println("salary : " + df.format(input)); //1205.64
}
}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:
- Title
- generate random number in java within a range without repeating with android studio
- Category
- Java
- Title
- usaco 2018 january contest
- Category
- Java
- Title
- android create notification
- Category
- Java
- Title
- programa que convierete un archi de c a java
- Category
- Java
- Title
- array null pointer java
- Category
- Java
- Title
- que es un length en java
- Category
- Java
- Title
- java public static void main
- Category
- Java
- Title
- array in java
- Category
- Java
- Title
- print map java
- Category
- Java
- Title
- java get keys from hashmap
- Category
- Java
- Title
- priority queue java reverse order
- Category
- Java
- Title
- compare list from db and list from request
- Category
- Java
- Title
- change the value in a hashtable java
- Category
- Java
- Title
- java count substring occurrences in string
- Category
- Java
- Title
- java mockito print called methods
- Category
- Java
- Title
- java for
- Category
- Java
- Title
- calculate days between two dates in java
- Category
- Java
- Title
- android studio keep device awake
- Category
- Java
- Title
- java stream sort Collator
- Category
- Java
- Title
- convert list to array in java
- Category
- Java
- Title
- counting the number of characters in a string java
- Category
- Java
- Title
- get current day java
- Category
- Java
- Title
- java variable string
- Category
- Java
- Title
- check if object is empty java 8
- Category
- Java
- Title
- java animated gif example
- Category
- Java
- Title
- java final meaning
- Category
- Java
- Title
- send variable intent
- Category
- Java
- Title
- java map iteration
- Category
- Java
- Title
- how to create a button in java
- Category
- Java
- Title
- java random number in range
- Category
- Java
- Title
- iterate through hashtable java
- Category
- Java
- Title
- java best way to concatenate strings
- Category
- Java
- Title
- saving String character in arraylist
- Category
- Java
- Title
- java stream find specific element
- Category
- Java
- Title
- android dismiss keyboard
- Category
- Java
- Title
- how to create a method java
- Category
- Java
- Title
- how to compare two maps in java
- Category
- Java
- Title
- java djikstra's algorithm
- Category
- Java
- Title
- convert array to list java
- Category
- Java
- Title
- how to convert string to double in android studio
- Category
- Java
- Title
- how to scan a character in java
- Category
- Java
- Title
- how to fill a list with a single value java
- Category
- Java
- Title
- print a letter in java
- Category
- Java
- Title
- java setter
- Category
- Java
- Title
- get raondom from array java
- Category
- Java
- Title
- create object of static class in java
- Category
- Java
- Title
- how to access private vairable in java method
- Category
- Java
- Title
- How to create a 2d array in java
- Category
- Java
- Title
- java prev permutation
- Category
- Java
- Title
- Java.awt graphics tutorial
- Category
- Java
- Title
- date to string java
- Category
- Java
- Title
- time conversion solution
- Category
- Java
- Title
- how get started with LWJGL 3
- Category
- Java
- Title
- print out value java
- Category
- Java
- Title
- java hahmap
- Category
- Java
- Title
- how to get a user to add to an arraylist in java
- Category
- Java
- Title
- java import text file into arraylist
- Category
- Java
- Title
- how to use pow function in java
- Category
- Java
- Title
- boolean checkbox swing
- Category
- Java
- Title
- restart application programmatically android
- Category
- Java
- Title
- how to cut a certion part from a string in java
- Category
- Java
- Title
- Jlabel icon
- Category
- Java
- Title
- java fx display simple rectangle
- Category
- Java
- Title
- working with buttons in applet java
- Category
- Java
- Title
- the type javax.servlet.ServletException cannot be resolved. It is indirectly referenced from required .class files
- Category
- Java
- Title
- java read lines from file
- Category
- Java
- Title
- java 11 initialize map
- Category
- Java
- Title
- java list get first element
- Category
- Java
- Title
- java shuffle list
- Category
- Java
- Title
- Java array equals
- Category
- Java
- Title
- java string split underscore
- Category
- Java
- Title
- how to run java program in linux server
- Category
- Java
- Title
- caesar cipher java
- Category
- Java
- Title
- crit chance in java
- Category
- Java
- Title
- how to install java 8 in ubuntu 16.04
- Category
- Java
- Title
- programmation android avoir acces à la liste des intents de partage
- Category
- Java
- Title
- objectoutputstream exemple
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- java 8 seconds to days
- Category
- Java
- Title
- load contents of file into string java
- Category
- Java
- Title
- java script using navigator.useragent to detect the browse
- Category
- Java
- Title
- how to add jar in maven java application in netbeans
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- long input in JAVA
- Category
- Java
- Title
- java boolean zen
- Category
- Java
- Title
- how to declare string array in java
- Category
- Java
- Title
- Primitive Type vs. Reference Type
- Category
- Java
- Title
- java for in loop
- Category
- Java
- Title
- java console text color
- Category
- Java
- Title
- spigot despawn entity
- Category
- Java
- Title
- iterate trough linked list java
- Category
- Java
- Title
- exception handling and reprompting
- Category
- Java
- Title
- alfred workflow
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- Java system load from resources
- Category
- Java
- Title
- run static method java
- Category
- Java
- Title
- copy array in java
- Category
- Java
- Title
- java "->"
- Category
- Java
- Title
- java script print date in YYYY-MM-DD HH:MM:SS format
- Category
- Java
- Title
- login and logout react native and firebase
- Category
- Java