
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Precision Handling in Java
Let us see how precisions are handled in Java −
Example
import java.io.*; import java.lang.*; public class Demo{ public static void main(String[] args){ double my_val = 34.909; System.out.println("The formatted value of 34.909 is "); System.out.println(String.format("%.7f", my_val)); double my_val_2 = 12.56; System.out.println("The formatted value of 12.56 is "); System.out.println(String.format("%.9f", my_val_2)); } }
Output
The formatted value of 34.909 is 34.9090000 The formatted value of 12.56 is 12.560000000
A class named Demo contains the main function, where a double valued integer is declared, and it is formatted by specifying the number that it should be formatted to. Similarly, another double variable is defined, and formatted and printed on the screen.
Advertisements