Java Code Examples for java.math.RoundingMode#HALF_UP
The following examples show how to use
java.math.RoundingMode#HALF_UP .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BigDecimalArithmeticTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * remainder(BigDecimal, MathContext) */ public void testRemainderMathContextHALF_UP() { String a = "3736186567876876578956958765675671119238118911893939591735"; int aScale = 45; String b = "134432345432345748766876876723342238476237823787879183470"; int bScale = 10; int precision = 15; RoundingMode rm = RoundingMode.HALF_UP; MathContext mc = new MathContext(precision, rm); String res = "3736186567876.876578956958765675671119238118911893939591735"; int resScale = 45; BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); BigDecimal result = aNumber.remainder(bNumber, mc); assertEquals("incorrect quotient value", res, result.toString()); assertEquals("incorrect quotient scale", resScale, result.scale()); }
Example 2
Source File: TestBase.java From OSPREY3 with GNU General Public License v2.0 | 6 votes |
static EpsilonApplier<BigDecimalBounds> bigDecimalBoundsAbsolute(double epsilon) { return new EpsilonApplier<BigDecimalBounds>(epsilon) { // use a math context with fixed precision to keep additions from being really slow! MathContext mathContext = new MathContext(32, RoundingMode.HALF_UP); BigDecimal bigEpsilon = MathTools.biggen(epsilon); @Override public String term() { return "absolutely"; } @Override public BigDecimalBounds apply(BigDecimalBounds bounds) { return new BigDecimalBounds( bounds.lower.subtract(bigEpsilon, mathContext), bounds.upper.add(bigEpsilon, mathContext) ); } }; }
Example 3
Source File: MonetaryFormat.java From green_android with GNU General Public License v3.0 | 6 votes |
public MonetaryFormat() { // defaults this.negativeSign = '-'; this.positiveSign = 0; // none this.zeroDigit = '0'; this.decimalMark = '.'; this.minDecimals = 2; this.decimalGroups = null; this.shift = 0; this.roundingMode = RoundingMode.HALF_UP; this.codes = new String[MAX_DECIMALS]; this.codes[0] = CODE_BTC; this.codes[3] = CODE_MBTC; this.codes[6] = CODE_UBTC; this.codeSeparator = ' '; this.codePrefixed = true; }
Example 4
Source File: RoundEvaluator.java From cql_engine with Apache License 2.0 | 6 votes |
public static Object round(Object operand, Object precision) { RoundingMode rm = RoundingMode.HALF_UP; if (operand == null) { return null; } if (operand instanceof BigDecimal){ if (((BigDecimal) operand).compareTo(new BigDecimal(0)) < 0) { rm = RoundingMode.HALF_DOWN; } if (precision == null || ((Integer) precision == 0)) { return ((BigDecimal) operand).setScale(0, rm); } else { return ((BigDecimal) operand).setScale((Integer)precision, rm); } } throw new InvalidOperatorArgument( "Round(Decimal) or Round(Decimal, Integer)", String.format("Round(%s%s)", operand.getClass().getName(), precision == null ? "" : ", " + precision.getClass().getName()) ); }
Example 5
Source File: BigDecimalArithmeticTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * Multiply two numbers of different scales using MathContext */ public void testMultiplyMathContextDiffScaleNegPos() { String a = "488757458676796558668876576576579097029810457634781384756794987"; int aScale = -63; String b = "747233429293018787918347987234564568"; int bScale = 63; String c = "3.6521591193960361339707130098174381429788164316E+98"; int cScale = -52; BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); MathContext mc = new MathContext(47, RoundingMode.HALF_UP); BigDecimal result = aNumber.multiply(bNumber, mc); assertEquals("incorrect value", c, result.toString()); assertEquals("incorrect scale", cScale, result.scale()); }
Example 6
Source File: TestBigDecimalIO.java From OSPREY3 with GNU General Public License v2.0 | 5 votes |
@Test public void fixed() throws IOException { BigDecimalIO.Fixed io = new BigDecimalIO.Fixed(new MathContext(16, RoundingMode.HALF_UP)); Buf buf = new Buf(); io.write(buf.out, BigDecimal.ONE); io.write(buf.out, BigDecimal.ZERO); io.write(buf.out, new BigDecimal("2.0")); io.write(buf.out, new BigDecimal("4.2")); io.write(buf.out, new BigDecimal("1.94538e-4893")); io.write(buf.out, MathTools.BigNaN); io.write(buf.out, MathTools.BigNegativeInfinity); io.write(buf.out, MathTools.BigPositiveInfinity); assertThat(buf.size(), is(io.numBytes*8)); buf.swap(); assertThat(io.read(buf.in), is(BigDecimal.ONE)); assertThat(io.read(buf.in), is(BigDecimal.ZERO)); assertThat(io.read(buf.in), is(new BigDecimal("2.0"))); assertThat(io.read(buf.in), is(new BigDecimal("4.2"))); assertThat(io.read(buf.in), is(new BigDecimal("1.94538e-4893"))); assertThat(io.read(buf.in), is(MathTools.BigNaN)); assertThat(io.read(buf.in), is(MathTools.BigNegativeInfinity)); assertThat(io.read(buf.in), is(MathTools.BigPositiveInfinity)); }
Example 7
Source File: AbstractNumberCellConverterFactory.java From xlsmapper with Apache License 2.0 | 5 votes |
/** * アノテーションを元に、{@link MathContext}のインスタンスを取得する。 * <p>有効桁数、丸め方法を設定したものを返す。 * <p>有効桁数は、デフォルトでは無期限にする。 * <p>丸め方法は、Excelに合わせて、{@link RoundingMode#HALF_UP}で固定。 * @param convertAnno * @return */ private MathContext createMathContext(final Optional<XlsNumberConverter> convertAnno) { if(convertAnno.isPresent() && convertAnno.get().precision() > 0) { return new MathContext(convertAnno.get().precision(), RoundingMode.HALF_UP); } else { //アノテーションがない場合は、制限なし。 return MathContext.UNLIMITED; } }
Example 8
Source File: BigDecimalValue.java From oopsla15-artifact with Eclipse Public License 1.0 | 5 votes |
@Override public IReal divide(IReal other, int precision){ // make sure the precision is *at least* the same as that of the arguments precision = Math.max(Math.max(value.precision(), other.precision()), precision); MathContext mc = new MathContext(precision, RoundingMode.HALF_UP); return BigDecimalValue.newReal(value.divide(((BigDecimalValue) other).value, mc)); }
Example 9
Source File: BigDecimalArithmeticTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * round(BigDecimal, MathContext) */ public void testRoundMathContextHALF_UP() { String a = "3736186567876876578956958765675671119238118911893939591735"; int aScale = 45; int precision = 15; RoundingMode rm = RoundingMode.HALF_UP; MathContext mc = new MathContext(precision, rm); String res = "3736186567876.88"; int resScale = 2; BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); BigDecimal result = aNumber.round(mc); assertEquals("incorrect quotient value", res, result.toString()); assertEquals("incorrect quotient scale", resScale, result.scale()); }
Example 10
Source File: Translation.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 4 votes |
@Override public String toString() { MathContext cont = new MathContext(4, RoundingMode.HALF_UP); return "Translation(" + new BigDecimal(vec.x, cont) + ", " + new BigDecimal(vec.y, cont) + ", " + new BigDecimal(vec.z, cont) + ")"; }
Example 11
Source File: Rotation.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 4 votes |
@Override public String toString() { MathContext cont = new MathContext(4, RoundingMode.HALF_UP); return "Rotation(" + new BigDecimal(angle, cont) + ", " + new BigDecimal(axis.x, cont) + ", " + new BigDecimal(axis.y, cont) + ", " + new BigDecimal(axis.z, cont) + ")"; }
Example 12
Source File: Translation.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 4 votes |
@Override public String toString() { MathContext cont = new MathContext(4, RoundingMode.HALF_UP); return "Translation(" + new BigDecimal(vec.x, cont) + ", " + new BigDecimal(vec.y, cont) + ", " + new BigDecimal(vec.z, cont) + ")"; }
Example 13
Source File: RoundingModeTests.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
public static void main(String [] argv) { // For each member of the family, make sure // rm == valueOf(rm.toString()) for(RoundingMode rm: RoundingMode.values()) { if (rm != RoundingMode.valueOf(rm.toString())) { throw new RuntimeException("Bad roundtrip conversion of " + rm.toString()); } } // Test that mapping of old integers to new values is correct if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) != RoundingMode.CEILING) { throw new RuntimeException("Bad mapping for ROUND_CEILING"); } if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) != RoundingMode.DOWN) { throw new RuntimeException("Bad mapping for ROUND_DOWN"); } if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) != RoundingMode.FLOOR) { throw new RuntimeException("Bad mapping for ROUND_FLOOR"); } if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) != RoundingMode.HALF_DOWN) { throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN"); } if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) != RoundingMode.HALF_EVEN) { throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN"); } if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) != RoundingMode.HALF_UP) { throw new RuntimeException("Bad mapping for ROUND_HALF_UP"); } if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) != RoundingMode.UNNECESSARY) { throw new RuntimeException("Bad mapping for ROUND_UNNECESARY"); } }
Example 14
Source File: Vertex5.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 4 votes |
public String toString() { MathContext cont = new MathContext(4, RoundingMode.HALF_UP); return "Vertex: (" + new BigDecimal(vec.x, cont) + ", " + new BigDecimal(vec.y, cont) + ", " + new BigDecimal(vec.z, cont) + ") " + "(" + new BigDecimal(uv.u, cont) + ", " + new BigDecimal(uv.v, cont) + ") ("+uv.tex+")"; }
Example 15
Source File: Scale.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 4 votes |
@Override public String toString() { MathContext cont = new MathContext(4, RoundingMode.HALF_UP); return "Scale(" + new BigDecimal(factor.x, cont) + ", " + new BigDecimal(factor.y, cont) + ", " + new BigDecimal(factor.z, cont) + ")"; }
Example 16
Source File: RoundDecimalExpression.java From phoenix with BSD 3-Clause "New" or "Revised" License | 4 votes |
protected RoundingMode getRoundingMode() { return RoundingMode.HALF_UP; }
Example 17
Source File: RoundingModeTests.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
public static void main(String [] argv) { // For each member of the family, make sure // rm == valueOf(rm.toString()) for(RoundingMode rm: RoundingMode.values()) { if (rm != RoundingMode.valueOf(rm.toString())) { throw new RuntimeException("Bad roundtrip conversion of " + rm.toString()); } } // Test that mapping of old integers to new values is correct if (RoundingMode.valueOf(BigDecimal.ROUND_CEILING) != RoundingMode.CEILING) { throw new RuntimeException("Bad mapping for ROUND_CEILING"); } if (RoundingMode.valueOf(BigDecimal.ROUND_DOWN) != RoundingMode.DOWN) { throw new RuntimeException("Bad mapping for ROUND_DOWN"); } if (RoundingMode.valueOf(BigDecimal.ROUND_FLOOR) != RoundingMode.FLOOR) { throw new RuntimeException("Bad mapping for ROUND_FLOOR"); } if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_DOWN) != RoundingMode.HALF_DOWN) { throw new RuntimeException("Bad mapping for ROUND_HALF_DOWN"); } if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_EVEN) != RoundingMode.HALF_EVEN) { throw new RuntimeException("Bad mapping for ROUND_HALF_EVEN"); } if (RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) != RoundingMode.HALF_UP) { throw new RuntimeException("Bad mapping for ROUND_HALF_UP"); } if (RoundingMode.valueOf(BigDecimal.ROUND_UNNECESSARY) != RoundingMode.UNNECESSARY) { throw new RuntimeException("Bad mapping for ROUND_UNNECESARY"); } }
Example 18
Source File: RoundDecimalExpression.java From phoenix with Apache License 2.0 | 4 votes |
protected RoundingMode getRoundingMode() { return RoundingMode.HALF_UP; }
Example 19
Source File: UVRotation.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 4 votes |
@Override public String toString() { MathContext cont = new MathContext(4, RoundingMode.HALF_UP); return "UVRotation(" + new BigDecimal(angle, cont) + ")"; }
Example 20
Source File: DefaultCashRounding.java From jsr354-ri with Apache License 2.0 | 2 votes |
/** * Creates an {@link MonetaryOperator} for rounding {@link MonetaryAmount} * instances given a currency. * * @param currency The currency, which determines the required precision. As * {@link RoundingMode}, by default, {@link RoundingMode#HALF_UP} * is used. */ DefaultCashRounding(CurrencyUnit currency, int minimalMinors) { this(currency, RoundingMode.HALF_UP, minimalMinors); }