Use a DecimalFormatter:
double number = 0.9999999999999; DecimalFormat numberFormat = new DecimalFormat("#.00"); System.out.println(numberFormat.format(number));
Will give you “0.99”. You can add or subtract 0 on the right side to get more or less decimals.
Or use ‘#’ on the right to make the additional digits optional, as in with #.## (0.30) would drop the trailing 0 to become (0.3).
Related Posts:
- How do I limit the number of decimals printed for a double?
- How do I convert a String to an int in Java?
- Java – Convert integer to string [duplicate]
- Convert String to double in Java
- How can I convert a char to int in Java? [duplicate]
- Convert int to char in java
- “int cannot be dereferenced” in Java
- Converting double to integer in Java
- Java JDK – possible lossy conversion from double to int
- Round a double to 2 decimal places [duplicate]
- Round a double to 2 decimal places [duplicate]
- How can I prevent java.lang.NumberFormatException: For input string: “N/A”?
- Round a double to 2 decimal places
- Double array initialization in Java
- How to check if an int is a null
- How to check whether a int is not null or empty?
- What is the difference between Integer and int in Java?
- Double decimal formatting in Java
- Double cannot be dereferenced?
- How to implement infinity in Java?
- How can I truncate a double to only two decimal places in Java?
- Java double.MAX_VALUE?
- How can I truncate a double to only two decimal places in Java?
- Java, Simplified check if int array contains int
- JAVA Variable declaration not allowed here
- Java Array Sort descending?
- What do F and D mean at the end of numeric literals?
- Determine if a String is an Integer in Java [duplicate]
- Convert boolean to int in Java
- Java ArrayList of Doubles
- How to Convert Int to Unsigned Byte and Back
- Java – Change int to ascii
- What is the difference between “Double” and “double” in Java?
- Convert float to double without losing precision
- Convert double to float in Java
- Problem with gif with transparent background
- Search for words with telephone numbers from 2-3-4 tree
- What is the reason behind “non-static method cannot be referenced from a static context”? [duplicate]
- Java string to date conversion
- Creating a “logical exclusive or” operator in Java
- Cannot make a static reference to the non-static method fxn(int) from the type Two [duplicate]
- How can I use pointers in Java?
- JavaFX – Exception in Application start method?
- What is IllegalStateException?
- Long vs Integer, long vs int, what to use and when?
- Java Using Nodes with LinkedList
- When is the @JsonProperty property used and what is it used for?
- “No X11 DISPLAY variable” – what does it mean?
- en_US or en-US, which one should you use?
- What is the difference between String.subString() and String.subSequence()
- Why Maven project is tied to J2SE-1.5 by default?
- Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in Java
- Exception in thread “main” java.lang.UnsupportedClassVersionError: a (Unsupported major.minor version 51.0)
- How to compare two double values in Java?
- Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0 error in Java
- How to override compareTo (Java)
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- package does not exist error!
- Required: Variable Found: Value
- What does the colon (:) operator do?
- Shifting array to the right – homework
- How to get rid of Checkstyle message ‘File does not end with a newline.’
- Uri not Absolute exception getting while calling Restful Webservice
- Very Basic Java
- SQLRecoverableException: I/O Exception: Connection reset
- What exactly is a Maven Snapshot and why do we need it?
- How can I edit a .jar file?
- java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver Exception occurring. Why?
- JAVA_HOME should point to a JDK not a JRE
- This Activity already has an action bar supplied by the window decor?
- Convert hex string to int
- Orphaned Case Error in Java
- Proxy Error 502 : The proxy server received an invalid response from an upstream server
- how to do a system pause in java for debugging?
- Immutable class?
- How to make a countdown timer in Java
- (Java) Tic-Tac-Toe game using 2 dimensional Array
- Incompatible types List of List and ArrayList of ArrayList
- Dice Rolling java program
- int cannot be converted to int []
- Joda DateTime to Timestamp conversion
- break statement in “if else” – java
- How to use a Do-while loop that continuously prompts a user?
- Java equivalent of unsigned long long?
- Getting “unixtime” in Java
- How do I break out of nested loops in Java?
- How do I convert this for loop into a while loop?
- Java switch statement: Constant expression required, but it IS constant
- Resolving File paths – ‘File not found’ error in Eclipse
- Simple Coin Toss using random class in Java. The do while loop doesn’t seem to generate random results
- Append a single character to a string or char array in java?
- Preventing a Java class from being instantiated and inherited
- Collision Detection between two images in Java
- What does .pack() do?
- String is immutable. What exactly is the meaning?
- How do I autoindent in Netbeans?
- What is an attribute in Java?
- MessageBodyWriter not found for media type=application/json
- Conversion from Long to Double in Java
- Failed to find Java VM