

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Format double type in Java
Let’s say we have the following three values −
double val1 = 15.546; double val2 = 25.87; double val3 = Math.PI;
Let us now format these double-type numbers. Firstly, we are formatting Euler’s number with Math.exp(). After that, we have also evaluated log. The %.3f you can see here is what we used for formatting the numbers.
System.out.printf("exp(%.3f) = %.3f%n", val1, Math.exp(val1)); System.out.printf("log = %.3f%n", val1, Math.log(val1));
The following is an example where we have also shown other ways to format double in Java.
Example
public class Demo { public static void main(String args[]) { double val1 = 15.546; double val2 = 25.87; double val3 = Math.PI; System.out.format("%f%n", val1); System.out.printf("exp(%.3f) = %.3f%n", val1, Math.exp(val1)); System.out.printf("log = %.3f%n", val1, Math.log(val1)); System.out.format("%3f%n", val2); System.out.format("%+3f%n", val2); System.out.format("%f%n", val3); System.out.format("%.3f%n", val3); System.out.printf("pow(%.3f, %.3f) = %.3f%n", val1, val2, Math.pow(val1, val2)); } }
Output
15.546000 exp(15.546) = 5643415.357 log = 15.546 25.870000 +25.870000 3.141593 3.142 pow(15.546, 25.870) = 6716991850252629000000000000000.000
- Related Questions & Answers
- Convert double primitive type to a Double object in Java
- Generate Random double type number in Java
- When can a double-type be preferred over float-type in Java?
- String format for Double in C#
- Convert a String to a double type number in Java
- Format double with new DecimalFormat("0.#####E0") in Java
- Explain double column cash book and its format
- How to convert a double value into a Java String using format method?
- Double brace initialization in Java
- Double isInfinite() method in Java
- Double isNaN() method in Java
- How to Convert Double to String to Double in Java?
- Format Month in MM format in Java
- Format Minutes in mm format in Java
- Format Month in M format in Java
Advertisements