
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
Format Negative Number Output with Parentheses in Java
A negative number output can be shown using the Formatter object −
Formatter f = new Formatter(); f.format("%12.2f", -7.598); System.out.println(f);
Try the below given code to format a Negative Number Output with Parentheses −
Formatter f = new Formatter(); f.format("%(d", -50); System.out.println(f);
The following is an example −
Example
import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("% d", 50); System.out.println(f); // negative number inside parentheses f = new Formatter(); f.format("%(d", -50); System.out.println(f); } }
Output
50 (50)
Advertisements