commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 33997] New: - optional precision round/trunc methods
Date Mon, 14 Mar 2005 05:05:04 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=33997>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=33997

           Summary: optional precision round/trunc methods
           Product: Commons
           Version: 2.1 Final
          Platform: Macintosh
        OS/Version: Mac OS X 10.0
            Status: NEW
          Severity: normal
          Priority: P3
         Component: Lang
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: bayard@apache.org


With 2.1 going out, I don't want to confuse things with new development, but
wanted to store this as something to suggest adding for 2.2/3.0. The JDK lacks a
method to round a number to a specified number of decimal places. Their may be a
quicker way than the below, which is quite daft, but due to worries about
overflow I've gone with the simplest approach:

    private double round(double value, int precision) {
        double valueNum = Math.floor(value);
        double decimal = value - valueNum;
        for(int i=precision; i>0; i--) {
            decimal *= 10;
        }
        decimal = Math.round(decimal);
        for(int i=precision; i>0; i--) {
            decimal /= 10;
        }
        return valueNum + decimal;
    }

The middle line may be changed from Math.round to Math.ceil and Math.floor to
achieve those variants. The for loops could be replaced with Math.pow(10,
precision), but I'm worried that that number may be too large for the type.
Needs some thought. Might be that java.text.DecimalFormat is seen as good enough.

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message