Home pageFind It!Contact JeksJeks documentation

Jeks

 Jeks forum

This forum is dedicated to Jeks.
You may read freely the messages it contents. If you want to write a message or answer to a subject, subscribe to it first.

Subjects Recent messages Login Subscribe

Messages of subject Set decimal precision

edmundo

Location : México
Member since : Sep 28, 2004
Messages : 1
 Sep 28, 2004 at 6:08 PM
Is there a way to set a 2 decimal precision to a jeks table, I have a process than generates the payroll with 5 decimals but I need to store only 2, because of problems with the cents.

I'd appreciate your help. Thanks.
---
Edmundo César Díaz de León

Manu

Location : Paris / France
Member since : Apr 29, 2003
Messages : 394
 Oct 5, 2004 at 9:43 PM
If you just want to display values with 2 decimals, you could change table cell renderer for numbers this way :

final TableCellRenderer defaultNumberRenderer = table.getDefaultRenderer(Number.class);
final DecimalFormat twoDecimalsFormat = new DecimalFormat ("0.00");
TableCellRenderer twoDecimalsRenderer = new TableCellRenderer ()
{
  public Component getTableCellRendererComponent (JTable table, Object value,
     boolean isSelected, boolean hasFocus, int row, int column)
  {
    // Format number before sending it two JTable default number renderer
    value = twoDecimalsFormat.format (value);
    return defaultNumberRenderer.getTableCellRendererComponent (table, value,
      isSelected, hasFocus, row, column);
  }
};
table.setDefaultRenderer(Number.class, twoDecimalsRenderer);
table.updateUI();

If you want to store and use numbers with exactly 2 decimals, you should use BigDecimal values instead of Double values in table model, meaning you should create a new Interpreter class able to compute BigDecimal values, override parseLitteral method in a JeksExpressionParser subclass to parse numbers as BigDecimal instances, and finally use this subclass in JeksTable constructor.
---
Manu (moderator/modérateur)


Home pageFind It!ContactTop

© Copyrights 1997-2023 eTeks - All rights reserved

JeksJeks documentation