JDK-4878207 : request for new numeric methods round and truncate
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2003-06-13
  • Updated: 2017-05-19
  • Resolved: 2016-08-22
Related Reports
Relates :  
Description
Name: nt126004			Date: 06/12/2003


A DESCRIPTION OF THE REQUEST :
I am requesting that 2 new methods be added.
They should be applicable to any numeric type
(including primitives) which contain both an
integer and decimal portion, e.g. double, Double,
etc.  The methods could be added to java.lang.Math.

The methods are:

round(n,d) where n is the number to round and d
is the number of positions after the decimal point.

Unlike the current java.lang.Math.round() method
(a) The return type should be the same as the input type.
    That is round(double,d) should return a double, not a long.
(b) the rounding position is variable.

If n is 0 then the number is rounded to the nearest
integer.  If n is less than 0 then the number is
rounded to the nearest power of 10.
e.g.
	round(123.4567,2)  returns 123.46
	round(123.4567,0)  returns 123
	round(123.4567,-1) returns 120

****

truncate(n,d) where n is the number to truncate and d
is the number of positions after the decimal point.

The return type should be the same as the input type.

If n is 0 then the number is truncated to the nearest
integer.  If n is less than 0 then the number is
truncated to the nearest power of 10.
e.g.
	truncate(123.4567,2)  returns 123.45 (no rounding)
	truncate(123.4567,0)  returns 123
	truncate(123.4567,-1) returns 120

  

JUSTIFICATION :
good to have
(Review ID: 187124) 
======================================================================
###@###.### 10/28/04 01:09 GMT

Comments
The concept of rounding to decimal digits for binary floating-point numbers is not completely well defined and likely to yield surprising results in practice. Closing as will not fix.
22-08-2016

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
14-06-2004

EVALUATION With the forthcoming functionality being introduced in java.math.BigDecimal, a user will be able to exactly convert a floating-point value (float or double) to BigDecimal and then round that BigDecimal to any number of decimal digits under any of 8 different rounding modes, including truncation and to-neareast rounding. This is the capability the submitter is requesting. However, there would also be utility in supporting *binary* rounding of floating-point values to any number number bits. A new round method, round(double d, MathContext mc) could take the same MathContext objects as the new methods in BigDecimal. A MathContext specifies the rounding mode and number of digits to use when computing the result. ###@###.### 2003-06-16 Decommitting from Tiger; functionality should be considered for a future release. ###@###.### 2003-09-08
16-06-2003

WORK AROUND For decimal rounding, use the rounding features that will be introduced in BigDecimal as part of jsr13 (see bug 4609098). ###@###.### 2003-06-16
16-06-2003