aspose file tools*
The moose likes Java in General and the fly likes How to format a number using country's currency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to format a number using country Watch "How to format a number using country New topic
Author

How to format a number using country's currency

Thiyagarajan Venkatachalam
Greenhorn

Joined: Sep 16, 2008
Posts: 4
Hi Everyone,

I want to format a number (like 999999999999999.99) with respect to country's currency code. For example, for US => the currency code 'USD' by taking as a parameter, the number should be formatted. Please let me know the classes and methods to use.

With Thanks,
Thiyagarajan
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Try



JDBCSupport - An easy to use, light-weight JDBC framework -
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Welcome to JavaRanch
You do realise that 999999999999999.99 will probably come out as 1000000000000000.00.
Thiyagarajan Venkatachalam
Greenhorn

Joined: Sep 16, 2008
Posts: 4

Thank you Sebastian and Campbell for your responses.

I tried utilizing the class suggested by you but I am getting the error "Exception: java.lang.NoSuchMethodError" when i used the method 'getCurrencyInstance()' in my class. I am not sure why it is unable to recognize it.

Moreover, when i checked the api for 'Locale' class, i came to know only parameter values like 'language,country or variant' can be passed as parameters. Also please help me how to format the number with the use of currency code (like 'USD') instead of these.

Waiting for your response.

With Thanks,
Thiyagarajan
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
You're welcome

Have you looked here or here for the currency codes. It would appear for Britain I would have to say Currency.getInstance("GBP"); using the factory method. I think you have a spelling error in the method you tried to use, it isn't getCurrencyInstance.
Thiyagarajan Venkatachalam
Greenhorn

Joined: Sep 16, 2008
Posts: 4
Hi Campbell,

Thanks for your immediate response

I have tried the factory method suggested by you 'Currency.getInstance(<currency code>)' but i got the error "Exception:
java.lang.NoClassDefFoundError: java/util/Currency
".

I found the class 'Currency' from the package 'java.util.Currency' and when i compiled my java class it does not gave me any error. But after implementing the same in the application, i got the above error.

Moreover i dont understand why the application is unable to recognize the class or method.

Need your help to proceed further.

Thank You,
Thiyagarajan
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

Thiyagarajan Venkatachalam wrote: I have tried the factory method suggested by you 'Currency.getInstance(<currency code>)' but i got the error "Exception:
java.lang.NoClassDefFoundError: java/util/Currency
".

That's strange. Class java.util.Currency was added in Java 1.4.

Are you running your code on a JRE older than Java 1.4 (but compiling it on JDK 1.4 or newer)?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3162
    
  10
Campbell Ritchie wrote:I think you have a spelling error in the method you tried to use, it isn't getCurrencyInstance.


It is if he's using the sample code that Sebastian posted.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
I was looking in Currency, Joanne. Sorry.
Thiyagarajan Venkatachalam
Greenhorn

Joined: Sep 16, 2008
Posts: 4
Hi Guys,

The application which i am working has the current version 1.3.1_19. I guess thats why the application was unable to recognize the classes(like 'java.util.Currency') and methods (like 'NumberFormat.getCurrencyInstance()').

Is there an alternate way to turn around to format the number value using country's currency ?

Will it cause an issue anytime if we write the logic for number formatting using "Decimal and Grouping" separators to format the numbers as if the predefined methods are not helping in my case ?

Jesper,

You are correct. Like what you said i am compiling the code in java version 1.4.2_04 but while running the same in the application in version 1.3.1_19 thats why i am getting the error.

Suggestions needed from you all to goahead.


Thank You,
Thiyagarajan
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3162
    
  10
Thiyagarajan Venkatachalam wrote:The application which i am working has the current version 1.3.1_19. I guess thats why the application was unable to recognize the classes(like 'java.util.Currency') and methods (like 'NumberFormat.getCurrencyInstance()').


The NumberFormat.getCurrencyInstance() method was in V 1.3.1, so that's not your problem. maybe you should post your code and the exact error message you get.

Edit - I've just read the rest of your post. Are the errors you are getting at runtime or compile time ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to format a number using country's currency
 
Similar Threads
Reading Currency Values
Localized Currency formatting issue
Client's Regional Settings in JSP
$ symbol not displayed properly in excel
double + double =?