• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How to format textfield into currency format?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14266
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17067
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17067
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic