File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Output Formatting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Output Formatting" Watch "Output Formatting" New topic

Output Formatting

Preethi Shetty

Joined: Feb 20, 2004
Posts: 11

I understand that there are two ways in which we can format the o/p.
1)Using the NumberFormat Class AND
2)Using the Decimal Format class.Using the Decimal format class we can create our own output formats.So i wrote down a very simple program to test this.But I came across a problem.

Here is my code
PS: corejava has the Console class.So please don't bother about it.My doubt lies with the DecimalFormat part of the code.

import corejava.*;
import java.text.*;

public class MySalary1
public static void main(String[] args)
String name,formatout;
int salary;
DecimalFormat df;
df= new DecimalFormat("Rs, 00,00,000.00");
name = Console.readLine("Enter your name");
salary = Console.readInt("Enter your salary");
formatout = df.format(salary);
System.out.println(name + " your salary is " + formatout);

Please see the format that I have decided to use. It is (Rs,00,00,000).This doesn't work.It always displays the o/p in (Rs 000,000,000,000.00) fashion.How do I display the output in the manner that I want?

Thanks in advance for the help.

Preethi Shetty

Courage does not always roar. Sometimes, it is the quiet voice at the end of the day saying, "I will try again tomorrow".
Steve Bradley

Joined: Jun 09, 2004
Posts: 15

I'm not sure what the Rs refers to in your argument to the constructor.

df= new DecimalFormat("Rs, 00,00,000.00");

This call would format the figure in a decimal format and provide a symbol for Japanese Yen...yes I'm getting off track but I was thinking that Rs refers to a foreign currency that is available in unicode?

df= new DecimalFormat(\u00A5###,###.##)
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
Rs is the currency symbol (short) for Rupees (Indian currency). Its a English symbol and not a single character. Its two characters 'R' and 's' in the plain text range. So it may not have a specific unicode number like yen(\u00A5)�A5;. But the diffrent regional Indian languages do have their currency symbols in Unicode.

I think she wants the commas to follow a pattern like 1,23,456.00

She is trying to fix the commas at 10**3 , 10**5 instead of 10**3, 10**6.

She is trying to follow the Hindi convention for commas in currencies(this convention is not used all the time).

I have not been able to get it myself and for the sake of curiosity would like to know if it is possible in Java.??? Seems it should be. Somehow it does not seem to be working.

I may be wrong about this but I suspect it wont work even if we use the Locale class and MessageFormat class. My guess is that the compiler cannot determine the position of commas in this arrangement when the number digits exceed the digits shown in the format. How many commas (and at what postion)in 10**9, 10**12 and so on. It may be a bug. But its more likely this is due to my limited knowledge than a bug. In any case its not a really big problem.

In any case I think this topic was beyond the syllabus for the exam. It suits some forum on internationalization in Java or advanced Java.

As a very tangential point of interest / debate let me add ():-

We Indians have so many languages and regional conventions. Fortunately on the number system we are all moving towards one uniform (English) system [which I am told originated in Asia or Arabia]. For the multiple languages and conventions available in India the documentation provided by is pretty good. Even there there is no documentation for these commas.

In any case a few of the symbols provided by for the different languages (at least for Tamil Language- charactermapping in tamil)are not even in common use so much so that the fonts available for each language (at least in case of Tamil which I tried out) dont even render some of the special(outdated) symbols (like -DAY(0bf3), MONTH=(0bf4), YEAR=(0bf5), DEBIT(0bf6), CREDIT(0bf7), ASABOVE(0bf8).[In fact in most tamil print magazines for numbers english numbers are used and the Tamil number characters seem to have been abandoned and would probably stump most people if used] ). Generally the fonts do a reasonably good job for most characters that are actually used.

So I guess one can be pragmatic about languages and not push for every aspect to be taken up. But it may not be not be agreeable to some people. For instance there seems to be a rule in India that Vehicle registation plates should read in english and not the regional languages but some people do like to insist on regional tradition (and dont mind breaking the rule) and make things complicated for rule makers and solution providers.

But then again this now takes the topic to a debate and maybe a better forum would be the dustbin because of my two cents value add.

[ June 10, 2004: Message edited by: Swamy Nathan ]
Preethi Shetty

Joined: Feb 20, 2004
Posts: 11

Thanks for the reply. Yes Swamy has totally understood my question. So looks like there is no way we can do what I wanted to acomplish. If anybody else can throw light on my question I would be more than happy.

I agree. Here's the link:
subject: Output Formatting
It's not a secret anymore!