• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to format a number using country's currency

 
Thiyagarajan Venkatachalam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try

 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch
You do realise that 999999999999999.99 will probably come out as 1000000000000000.00.
 
Thiyagarajan Venkatachalam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking in Currency, Joanne. Sorry.
 
Thiyagarajan Venkatachalam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic