• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

How to format textfield into currency format?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to format the number inputted in the text field into currency. For example, if the user enters 8 for the wage, the wage will be displayed as $0.08 in the output text area; a wage entered as 1055 would be displayed as $10.55.

 
Saloon Keeper
Posts: 10873
234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't parse the wage to a double, parse it to an int.

What you'll want to do consists of four steps:

1) Parse a string like "$3.50" to an int 350.
2) When the user presses a digit like 0 while the wage field has focus, append the digit to the int to make 3500.
3) When the user presses backspace while the wage field has focus, strip a digit from the int.
4) Format an int like 3500 to a string "$35.00".

Parsing and formatting you can easily do using NumberFormat.parse() and NumberFormat.format(). Get an instance using NumberFormat.getCurrencyInstance(Locale).

To append a digit to your integer wage, just perform wage * 10 + digit. You can use backspace to strip a digit. All you have to do is wage / 10.
 
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Don't parse the wage to a double, parse it to an int.


Agree

What you'll want to do consists of four steps:

1) Parse a string like "$3.50" to an int 350.
2) When the user presses a digit like 0 while the wage field has focus, append the digit to the int to make 3500.
3) When the user presses backspace while the wage field has focus, strip a digit from the int.
4) Format an int like 3500 to a string "$35.00".


That is more than what’s being instructed.

The instructions are to format “8” as “$0.08” and “1055” as “$10.55”. So really, only #4 is needed.

@OP: read the API documentation for System.out.printf()
 
Junilu Lacar
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note also that the result of integer division is an integer. That is, 25/100 is not 0.25; it’s 0.  To get a double, you need to do 25/100.0

100 is an int
100.0 is a double
 
Fire me boy! Cool, soothing, shameless self promotion:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!