Java Number formatting for many fields in the data object

I have a data object that is holding a meny BigDecimal object as well as String and other type of object in my data object class. what is the best way to do some common operation on the BigDecimal field like intrest rate

interest=interest.setScale(2, RoundingMode.HALF_UP);
  1. I could do it in the setters/ getter but it could be more erroneous I want to make sure that every variable has been treat by same method.

  2. I could have some helper class that could be called from the setter or setter. I hate to write any code in the helper.

Is there any other good way I could do this ?


I would use the setters for that. If I had to do that to all BigDecimal fields I would consider extend the BigDecimal and code the treatment on the constructor.

Why not have a static method in the class that formats the BigDecimals as you require?

public static BigDecimal formatAs(BigDecimal example) {
    //format here and return it

Then you can use it like this:

BigDecimal interest = MyClass.formatAs(myInstance.getInterest());

BigDecimal rate = MyClass.formatAs(myInstance.getRate());

For as many BigDecimals as you have in your class...

Need Your Help

Cropping PDF using BoundingBox/CropBox in Postscript

pdf postscript xpdf

I would like to know what the actual difference between BoundingBox and CropBox in a Postscript file is. I want to crop a PDF file and display only the cropped part of it as another PDF file. I con...

Django Admin: add custom fields to template pages

python django django-cms

I'm new to Django so this might be a simple question. I'm trying to build a portfolio site using Django. I'd like to have a homepage, portfolio page, portfolio detail pages, and contact page. There...