Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » scout » AbstractDoubleColumn: rounding decimal values(decimal values like 0.25 round to 0.2, which isn't mathematical correct)
AbstractDoubleColumn: rounding decimal values [message #909616] Fri, 07 September 2012 08:30 Go to next message
Birgit Körschgen is currently offline Birgit Körschgen
Messages: 1
Registered: September 2012
Junior Member
It seems that the method org.eclipse.scout.rt.client.ui.basic.table.columns.AbstractDoubleColumn.decorateCellInternal(Cell cell, ITableRow row) doesn't round decimal values correctly.
As an example: when you have a value 0.25 and maxFractionDigits set on 1, then you get 0.2. On my opinion it should be 0.3 (which would be mathematical correct).

The reason for this seems to be that java uses RoundingMode.HALF_EVEN as default (see also the javadoc for DecimalFormat and RoundingMode). This rounding mode "rounds towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor".

The following little program visualizes this:

public class XXX {
public static void main(String[] args) {
DecimalFormat f = (DecimalFormat) NumberFormat.getNumberInstance();
f.setGroupingUsed(true);
f.setRoundingMode(RoundingMode.HALF_EVEN);
//f.setRoundingMode(RoundingMode.HALF_UP);

f.setMinimumFractionDigits(0);
f.setMaximumFractionDigits(0);
System.out.println(f.format(22.4));
System.out.println(f.format(22.5));
System.out.println(f.format(22.6));
System.out.println();

f.setMinimumFractionDigits(1);
f.setMaximumFractionDigits(1);
System.out.println(f.format(2.24));
System.out.println(f.format(2.25));
System.out.println(f.format(2.26));
System.out.println();

f.setMinimumFractionDigits(2);
f.setMaximumFractionDigits(2);
System.out.println(f.format(0.224));
System.out.println(f.format(0.225));
System.out.println(f.format(0.226));
System.out.println();

}
}

So, why can't we do it with mathematic correctness?
Re: AbstractDoubleColumn: rounding decimal values [message #909631 is a reply to message #909616] Fri, 07 September 2012 08:50 Go to previous messageGo to next message
Ivan Motsch is currently offline Ivan Motsch
Messages: 100
Registered: March 2010
Senior Member
Thanks for the post.

This is about the debate "mathematical" correctness or "financial" correctness in java.
Basically scout cannot decide which one is better since it depends on the business logic of the project using it.
Therefore the default in scout is to use java "as is". And java uses the default ROUND_EVEN.

When thinking about setting the explicit ROUND_UP default in eclipse scout, there are two major issues.
1) scout then uses a different default than java and programmers might run into an issue where there are inconsistent behaviours.

2) we break existing functionality of end-users and developers that rely on that current default.

However also java is not very consistent: Math.round makes an implicit ROUND_UP.


Possible solution for scout to find a default value for runding would be
1. using a system property
2. using an osgi context service
3. overriding AbstractDoubleColumn#getNumberFormat


solution 1 and 2 require a scout change and depending on the customer requirements they are too global or too specific.

solution 1 would be my preferred one, since it defines a global JAVA default for rounding that could be used in all parts of the java application, not just scout ad eclipse.

solution 2 includes a service call for every number formatting and is very expensive.

solution 3 can be done by you at once, just add an override.

For solution 1 you would have to create bugzilla. Solution 3 is in place already.
Re: AbstractDoubleColumn: rounding decimal values [message #1220550 is a reply to message #909616] Thu, 12 December 2013 09:17 Go to previous messageGo to next message
Adrian Sacchi is currently offline Adrian Sacchi
Messages: 5
Registered: January 2013
Junior Member
Scout 3.10 introduces RoundingMode as property for all number fields and columns. The default for all decimal fields and columns is ROUND_HALF_UP.

See: https://wiki.eclipse.org/Scout/NewAndNoteworthy/3.10#Number_Fields_and_Columns
Re: AbstractDoubleColumn: rounding decimal values [message #1220762 is a reply to message #1220550] Fri, 13 December 2013 08:12 Go to previous message
Jeremie Bresson is currently offline Jeremie Bresson
Messages: 657
Registered: October 2011
Senior Member
This is great work!

Can you also mention the new property on the field and colum pages:
* DoubleField
* BigDecimalField
* DoubleColumn
* BigDecimalColumn

Maybe with a note indicating that we support for version > 3.10.0:
{{important|Required version|The API described here requires version 3.10.0 or bigger.}}


Thank you in advance.
Previous Topic:How To add Tabs to TabBox programmatically
Next Topic:Custom Logger
Goto Forum:
  


Current Time: Sat Aug 23 15:57:38 EDT 2014

Powered by FUDForum. Page generated in 0.02442 seconds