Almost everything I’ve ever read about Java says don’t use float or double for currency, but it seems like everything in Swing tries to force you to do exactly that if you want to input / display currency values that have decimal places (aka exactly what most people want to do).
I can use NumberFormat.getCurrencyInstance() with a NumberFormatter and JFormattedTextField, but I can’t figure out how to get it to display decimal places without using Float or Double. I’ve got to be overlooking something simple.
How can I make my currency fields display $1.55, accept input as 1.55 and store the input as an Integer with a value of 155 (cents)?
You cannot do math on formatters. What you should do is have a member variable of your pojo holding the price in cents. Something like:
Then you can have a getter which will return this in dollars. Something like:
This is what you’ll pass into your formatter.