LEFT | RIGHT |
(no file at all) | |
| 1 // © 2017 and later: Unicode, Inc. and others. |
| 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
| 3 package com.ibm.icu.impl.number.rounders; |
| 4 |
| 5 import java.math.BigDecimal; |
| 6 |
| 7 import com.ibm.icu.impl.number.FormatQuantity; |
| 8 import com.ibm.icu.impl.number.Properties; |
| 9 import com.ibm.icu.impl.number.Rounder; |
| 10 |
| 11 public class IncrementRounder extends Rounder { |
| 12 |
| 13 public static interface IProperties extends IBasicRoundingProperties { |
| 14 |
| 15 static BigDecimal DEFAULT_ROUNDING_INCREMENT = null; |
| 16 |
| 17 /** @see #setRoundingIncrement */ |
| 18 public BigDecimal getRoundingIncrement(); |
| 19 |
| 20 /** |
| 21 * Sets the increment to which to round numbers. For example, with a roundin
g interval of 0.05, |
| 22 * the number 11.17 would be formatted as "11.15" in locale <em>en-US</em> w
ith the default |
| 23 * rounding mode. |
| 24 * |
| 25 * <p>You can use either a rounding increment or significant digits, but not
both at the same |
| 26 * time. |
| 27 * |
| 28 * <p>The rounding increment can be specified in a pattern string. For examp
le, the pattern |
| 29 * "#,##0.05" corresponds to a rounding interval of 0.05 with 1 minimum inte
ger digit and a |
| 30 * grouping size of 3. |
| 31 * |
| 32 * @param roundingIncrement The interval to which to round. |
| 33 * @return The property bag, for chaining. |
| 34 */ |
| 35 public IProperties setRoundingIncrement(BigDecimal roundingIncrement); |
| 36 } |
| 37 |
| 38 public static boolean useRoundingIncrement(IProperties properties) { |
| 39 return properties.getRoundingIncrement() != IProperties.DEFAULT_ROUNDING_INC
REMENT; |
| 40 } |
| 41 |
| 42 private final BigDecimal roundingIncrement; |
| 43 |
| 44 public static IncrementRounder getInstance(IProperties properties) { |
| 45 return new IncrementRounder(properties); |
| 46 } |
| 47 |
| 48 private IncrementRounder(IProperties properties) { |
| 49 super(properties); |
| 50 if (properties.getRoundingIncrement().compareTo(BigDecimal.ZERO) <= 0) { |
| 51 throw new IllegalArgumentException("Rounding interval must be greater than
zero"); |
| 52 } |
| 53 roundingIncrement = properties.getRoundingIncrement(); |
| 54 } |
| 55 |
| 56 @Override |
| 57 public void apply(FormatQuantity input) { |
| 58 input.roundToIncrement(roundingIncrement, mathContext); |
| 59 applyDefaults(input); |
| 60 } |
| 61 |
| 62 @Override |
| 63 public void export(Properties properties) { |
| 64 super.export(properties); |
| 65 properties.setRoundingIncrement(roundingIncrement); |
| 66 } |
| 67 } |
LEFT | RIGHT |