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: [email protected]
        ReportedBy: [EMAIL PROTECTED]


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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to