jdk/src/share/classes/java/math/RoundingMode.java
changeset 18558 4c7fd49d28d0
parent 8162 d91ca3f845b0
child 20883 c6f4757cd58b
equal deleted inserted replaced
18557:3000599aac3e 18558:4c7fd49d28d0
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    99          * rounding mode never decreases the magnitude of the calculated
    99          * rounding mode never decreases the magnitude of the calculated
   100          * value.
   100          * value.
   101          *
   101          *
   102          *<p>Example:
   102          *<p>Example:
   103          *<table border>
   103          *<table border>
       
   104          * <caption><b>Rounding mode UP Examples</b></caption>
   104          *<tr valign=top><th>Input Number</th>
   105          *<tr valign=top><th>Input Number</th>
   105          *    <th>Input rounded to one digit<br> with {@code UP} rounding
   106          *    <th>Input rounded to one digit<br> with {@code UP} rounding
   106          *<tr align=right><td>5.5</td>  <td>6</td>
   107          *<tr align=right><td>5.5</td>  <td>6</td>
   107          *<tr align=right><td>2.5</td>  <td>3</td>
   108          *<tr align=right><td>2.5</td>  <td>3</td>
   108          *<tr align=right><td>1.6</td>  <td>2</td>
   109          *<tr align=right><td>1.6</td>  <td>2</td>
   122          * prior to a discarded fraction (i.e., truncates).  Note that this
   123          * prior to a discarded fraction (i.e., truncates).  Note that this
   123          * rounding mode never increases the magnitude of the calculated value.
   124          * rounding mode never increases the magnitude of the calculated value.
   124          *
   125          *
   125          *<p>Example:
   126          *<p>Example:
   126          *<table border>
   127          *<table border>
       
   128          * <caption><b>Rounding mode DOWN Examples</b></caption>
   127          *<tr valign=top><th>Input Number</th>
   129          *<tr valign=top><th>Input Number</th>
   128          *    <th>Input rounded to one digit<br> with {@code DOWN} rounding
   130          *    <th>Input rounded to one digit<br> with {@code DOWN} rounding
   129          *<tr align=right><td>5.5</td>  <td>5</td>
   131          *<tr align=right><td>5.5</td>  <td>5</td>
   130          *<tr align=right><td>2.5</td>  <td>2</td>
   132          *<tr align=right><td>2.5</td>  <td>2</td>
   131          *<tr align=right><td>1.6</td>  <td>1</td>
   133          *<tr align=right><td>1.6</td>  <td>1</td>
   146          * if negative, behaves as for {@code RoundingMode.DOWN}.  Note
   148          * if negative, behaves as for {@code RoundingMode.DOWN}.  Note
   147          * that this rounding mode never decreases the calculated value.
   149          * that this rounding mode never decreases the calculated value.
   148          *
   150          *
   149          *<p>Example:
   151          *<p>Example:
   150          *<table border>
   152          *<table border>
       
   153          * <caption><b>Rounding mode CEILING Examples</b></caption>
   151          *<tr valign=top><th>Input Number</th>
   154          *<tr valign=top><th>Input Number</th>
   152          *    <th>Input rounded to one digit<br> with {@code CEILING} rounding
   155          *    <th>Input rounded to one digit<br> with {@code CEILING} rounding
   153          *<tr align=right><td>5.5</td>  <td>6</td>
   156          *<tr align=right><td>5.5</td>  <td>6</td>
   154          *<tr align=right><td>2.5</td>  <td>3</td>
   157          *<tr align=right><td>2.5</td>  <td>3</td>
   155          *<tr align=right><td>1.6</td>  <td>2</td>
   158          *<tr align=right><td>1.6</td>  <td>2</td>
   170          * if negative, behave as for {@code RoundingMode.UP}.  Note that
   173          * if negative, behave as for {@code RoundingMode.UP}.  Note that
   171          * this rounding mode never increases the calculated value.
   174          * this rounding mode never increases the calculated value.
   172          *
   175          *
   173          *<p>Example:
   176          *<p>Example:
   174          *<table border>
   177          *<table border>
       
   178          * <caption><b>Rounding mode FLOOR Examples</b></caption>
   175          *<tr valign=top><th>Input Number</th>
   179          *<tr valign=top><th>Input Number</th>
   176          *    <th>Input rounded to one digit<br> with {@code FLOOR} rounding
   180          *    <th>Input rounded to one digit<br> with {@code FLOOR} rounding
   177          *<tr align=right><td>5.5</td>  <td>5</td>
   181          *<tr align=right><td>5.5</td>  <td>5</td>
   178          *<tr align=right><td>2.5</td>  <td>2</td>
   182          *<tr align=right><td>2.5</td>  <td>2</td>
   179          *<tr align=right><td>1.6</td>  <td>1</td>
   183          *<tr align=right><td>1.6</td>  <td>1</td>
   196          * {@code RoundingMode.DOWN}.  Note that this is the rounding
   200          * {@code RoundingMode.DOWN}.  Note that this is the rounding
   197          * mode commonly taught at school.
   201          * mode commonly taught at school.
   198          *
   202          *
   199          *<p>Example:
   203          *<p>Example:
   200          *<table border>
   204          *<table border>
       
   205          * <caption><b>Rounding mode HALF_UP Examples</b></caption>
   201          *<tr valign=top><th>Input Number</th>
   206          *<tr valign=top><th>Input Number</th>
   202          *    <th>Input rounded to one digit<br> with {@code HALF_UP} rounding
   207          *    <th>Input rounded to one digit<br> with {@code HALF_UP} rounding
   203          *<tr align=right><td>5.5</td>  <td>6</td>
   208          *<tr align=right><td>5.5</td>  <td>6</td>
   204          *<tr align=right><td>2.5</td>  <td>3</td>
   209          *<tr align=right><td>2.5</td>  <td>3</td>
   205          *<tr align=right><td>1.6</td>  <td>2</td>
   210          *<tr align=right><td>1.6</td>  <td>2</td>
   221          * fraction is &gt; 0.5; otherwise, behaves as for
   226          * fraction is &gt; 0.5; otherwise, behaves as for
   222          * {@code RoundingMode.DOWN}.
   227          * {@code RoundingMode.DOWN}.
   223          *
   228          *
   224          *<p>Example:
   229          *<p>Example:
   225          *<table border>
   230          *<table border>
       
   231          * <caption><b>Rounding mode HALF_DOWN Examples</b></caption>
   226          *<tr valign=top><th>Input Number</th>
   232          *<tr valign=top><th>Input Number</th>
   227          *    <th>Input rounded to one digit<br> with {@code HALF_DOWN} rounding
   233          *    <th>Input rounded to one digit<br> with {@code HALF_DOWN} rounding
   228          *<tr align=right><td>5.5</td>  <td>5</td>
   234          *<tr align=right><td>5.5</td>  <td>5</td>
   229          *<tr align=right><td>2.5</td>  <td>2</td>
   235          *<tr align=right><td>2.5</td>  <td>2</td>
   230          *<tr align=right><td>1.6</td>  <td>2</td>
   236          *<tr align=right><td>1.6</td>  <td>2</td>
   253          * the rounding policy used for {@code float} and {@code double}
   259          * the rounding policy used for {@code float} and {@code double}
   254          * arithmetic in Java.
   260          * arithmetic in Java.
   255          *
   261          *
   256          *<p>Example:
   262          *<p>Example:
   257          *<table border>
   263          *<table border>
       
   264          * <caption><b>Rounding mode HALF_EVEN Examples</b></caption>
   258          *<tr valign=top><th>Input Number</th>
   265          *<tr valign=top><th>Input Number</th>
   259          *    <th>Input rounded to one digit<br> with {@code HALF_EVEN} rounding
   266          *    <th>Input rounded to one digit<br> with {@code HALF_EVEN} rounding
   260          *<tr align=right><td>5.5</td>  <td>6</td>
   267          *<tr align=right><td>5.5</td>  <td>6</td>
   261          *<tr align=right><td>2.5</td>  <td>2</td>
   268          *<tr align=right><td>2.5</td>  <td>2</td>
   262          *<tr align=right><td>1.6</td>  <td>2</td>
   269          *<tr align=right><td>1.6</td>  <td>2</td>
   276          * result, hence no rounding is necessary.  If this rounding mode is
   283          * result, hence no rounding is necessary.  If this rounding mode is
   277          * specified on an operation that yields an inexact result, an
   284          * specified on an operation that yields an inexact result, an
   278          * {@code ArithmeticException} is thrown.
   285          * {@code ArithmeticException} is thrown.
   279          *<p>Example:
   286          *<p>Example:
   280          *<table border>
   287          *<table border>
       
   288          * <caption><b>Rounding mode UNNECESSARY Examples</b></caption>
   281          *<tr valign=top><th>Input Number</th>
   289          *<tr valign=top><th>Input Number</th>
   282          *    <th>Input rounded to one digit<br> with {@code UNNECESSARY} rounding
   290          *    <th>Input rounded to one digit<br> with {@code UNNECESSARY} rounding
   283          *<tr align=right><td>5.5</td>  <td>throw {@code ArithmeticException}</td>
   291          *<tr align=right><td>5.5</td>  <td>throw {@code ArithmeticException}</td>
   284          *<tr align=right><td>2.5</td>  <td>throw {@code ArithmeticException}</td>
   292          *<tr align=right><td>2.5</td>  <td>throw {@code ArithmeticException}</td>
   285          *<tr align=right><td>1.6</td>  <td>throw {@code ArithmeticException}</td>
   293          *<tr align=right><td>1.6</td>  <td>throw {@code ArithmeticException}</td>