Example usage for java.math BigDecimal ONE

List of usage examples for java.math BigDecimal ONE

Introduction

In this page you can find the example usage for java.math BigDecimal ONE.

Prototype

BigDecimal ONE

To view the source code for java.math BigDecimal ONE.

Click Source Link

Document

The value 1, with a scale of 0.

Usage

From source file:org.openhab.ui.classic.internal.render.SetpointRenderer.java

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Setpoint sp = (Setpoint) w;//from w w  w  .  j  av  a 2 s  . c  o m

    State state = itemUIRegistry.getState(w);
    String newLowerState = state.toString();
    String newHigherState = state.toString();

    // set defaults for min, max and step
    BigDecimal step = sp.getStep();
    if (step == null) {
        step = BigDecimal.ONE;
    }
    BigDecimal minValue = sp.getMinValue();
    if (minValue == null) {
        minValue = BigDecimal.ZERO;
    }
    BigDecimal maxValue = sp.getMaxValue();
    if (maxValue == null) {
        maxValue = BigDecimal.valueOf(100);
    }

    // if the current state is a valid value, we calculate the up and down step values
    if (state instanceof DecimalType || state instanceof QuantityType) {
        BigDecimal currentState;
        if (state instanceof DecimalType) {
            currentState = ((DecimalType) state).toBigDecimal();
        } else {
            currentState = ((QuantityType<?>) state).toBigDecimal();
        }
        BigDecimal newLower = currentState.subtract(step);
        BigDecimal newHigher = currentState.add(step);
        if (newLower.compareTo(minValue) < 0) {
            newLower = minValue;
        }
        if (newHigher.compareTo(maxValue) > 0) {
            newHigher = maxValue;
        }
        newLowerState = newLower.toString();
        newHigherState = newHigher.toString();

        if (state instanceof QuantityType) {
            newLowerState = newLowerState + " " + ((QuantityType<?>) state).getUnit().toString();
            newHigherState = newHigherState + " " + ((QuantityType<?>) state).getUnit().toString();
        }
    }

    String snippetName = "setpoint";
    String snippet = getSnippet(snippetName);

    snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
    snippet = StringUtils.replace(snippet, "%category%", getCategory(w));
    snippet = StringUtils.replace(snippet, "%item%", w.getItem());
    snippet = StringUtils.replace(snippet, "%state%", getState(w));
    snippet = StringUtils.replace(snippet, "%newlowerstate%", newLowerState);
    snippet = StringUtils.replace(snippet, "%newhigherstate%", newHigherState);
    snippet = StringUtils.replace(snippet, "%label%", getLabel(w));
    snippet = StringUtils.replace(snippet, "%state%", getState(w));
    snippet = StringUtils.replace(snippet, "%format%", getFormat());
    snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);
    snippet = StringUtils.replace(snippet, "%minValue%", minValue.toString());
    snippet = StringUtils.replace(snippet, "%maxValue%", maxValue.toString());
    snippet = StringUtils.replace(snippet, "%step%", step.toString());

    // Process the color tags
    snippet = processColor(w, snippet);

    sb.append(snippet);
    return null;
}

From source file:fakingXmocking.CurrencyConversionHttpClientFake.java

private String formatResultContainingCurrencyConversion(String[] params) {
    String from = getParameter(params, "from");
    String to = getParameter(params, "to");
    BigDecimal rate = findConversionRate(from, to);

    currenciesAndRates.put(from + '>' + to, rate.setScale(2));
    currenciesAndRates.put(to + '>' + from, BigDecimal.ONE.divide(rate, 2, RoundingMode.HALF_UP));

    return "<div id=\"converter_results\"><ul><li><b>1 " + from + " = " + rate + ' ' + to + "</b>";
}

From source file:com.qcadoo.model.internal.units.UnitConversionImpl.java

public static UnitConversion build(final String unit, final MathContext mathContext) {
    return new UnitConversionImpl(unit, unit, BigDecimal.ONE, mathContext);
}

From source file:cherry.foundation.type.SecureBigDecimalTest.java

@Test
public void testToString() {
    SecureBigDecimal sec = plainValueOf(BigDecimal.ONE);
    assertThat(sec.toString(), is("SecureBigDecimal[1]"));
}

From source file:com.xeiam.xchange.bitstamp.service.trade.PlaceLimitOrderJSONTest.java

@Test
public void testUnmarshal() throws IOException {

    // Read in the JSON from the example resources
    InputStream is = PlaceLimitOrderJSONTest.class.getResourceAsStream("/trade/example-place-limit-order.json");

    // Use Jackson to parse it
    ObjectMapper mapper = new ObjectMapper();
    BitstampOrder newOrder = mapper.readValue(is, BitstampOrder.class);

    assertThat(newOrder.getId()).isEqualTo(1273070);
    assertThat(newOrder.getAmount()).isEqualTo(BigDecimal.ONE);
    assertThat(newOrder.getPrice()).isEqualTo(new BigDecimal("1.25"));
    assertThat(newOrder.getType()).isEqualTo(0);
}

From source file:nl.tudelft.stocktrader.util.StockTraderUtility.java

public static BigDecimal getRandomPriceChangeFactor(BigDecimal currentPrice) {
    if (currentPrice.compareTo(PENNY_STOCK_P) == -1 || currentPrice.compareTo(PENNY_STOCK_P) == 0) {
        return JUNK_STOCK_MIRACLE_MULTIPLIER;
    } else if (currentPrice.compareTo(STOCK_P_HIGH_BAR) == 1 || currentPrice.compareTo(STOCK_P_HIGH_BAR) == 0) {
        return STOCK_P_HIGH_BAR_CRASH;
    }/*from w  ww .ja va2  s  . co  m*/

    BigDecimal factor = BigDecimal.valueOf(0);
    Random rand = new Random();
    int y = rand.nextInt(STOCK_CHANGE_MAX_PERCENT.subtract(BigDecimal.ONE).intValue());
    y = y + 1;
    int x = rand.nextInt();

    if (x % 2 == 0) {
        factor = BigDecimal.ONE.subtract((BigDecimal.valueOf(y)).divide(BigDecimal.valueOf(100)));// / 100m;
    } else
        factor = BigDecimal.ONE.add(BigDecimal.ONE.add(BigDecimal.valueOf(y).divide(BigDecimal.valueOf(100))));
    return factor;
}

From source file:io.curly.advisor.model.Review.java

private BigDecimal fixPrecision(BigDecimal rate) {
    if (rate == null)
        return BigDecimal.ZERO;
    BigDecimal decimal = rate.remainder(BigDecimal.ONE);
    BigInteger integer = rate.toBigInteger();
    if (decimal.compareTo(new BigDecimal(0.5)) > 0) {
        return new BigDecimal(integer.add(new BigInteger("1")));
    } else if (decimal.compareTo(new BigDecimal(0.5)) < 0) {
        return new BigDecimal(integer);
    }//from w  w w. ja va  2 s.c o  m
    return rate;
}

From source file:org.impotch.calcul.impot.cantonal.ge.pp.BaremeRevenuPallier2009Test.java

@Before
public void setUp() throws Exception {
    SimpleFournisseurIndicePeriodique fournisseur = new SimpleFournisseurIndicePeriodique();
    Map<Integer, BigDecimal> indices = new HashMap<Integer, BigDecimal>();
    indices.put(2009, BigDecimal.ONE);
    fournisseur.setIndices(indices);/*from   w  w w .j  a va2  s.  co m*/
    ConstructeurBaremeIndexeTxMarginalConstantParTranche constructeur = new ConstructeurBaremeIndexeTxMarginalConstantParTranche();
    constructeur.validite(2009);
    constructeur.anneeReferenceRencherissement(2009);
    constructeur.indexateur(fournisseur);
    constructeur.tranche(17493, "0 %");
    constructeur.tranche(21076, "8 %");
    constructeur.tranche(23184, "9 %");
    constructeur.tranche(25291, "10 %");
    constructeur.tranche(27399, "11 %");
    constructeur.tranche(32668, "12 %");
    constructeur.tranche(36883, "13 %");
    constructeur.tranche(41099, "14 %");
    constructeur.tranche(45314, "14.5 %");
    constructeur.tranche(72713, "15 %");
    constructeur.tranche(119081, "15.5 %");
    constructeur.tranche(160179, "16 %");
    constructeur.tranche(181256, "16.5 %");
    constructeur.tranche(259238, "17 %");
    constructeur.tranche(276099, "17.5 %");
    constructeur.tranche(388857, "18 %");
    constructeur.tranche(609103, "18.5 %");
    constructeur.derniereTranche("19 %");
    bareme = constructeur.typeArrondiTranche(TypeArrondi.CINQ_CTS).construire(2009);
}

From source file:py.una.pol.karaku.dao.entity.interceptors.BigDecimalInterceptor.java

@Override
public void intercept(Operation o, Field field, Object bean) {

    BigDecimal value = (BigDecimal) ReflectionUtils.getField(field, bean);
    if (value == null) {
        return;/*ww  w .j a  va2 s. c  om*/
    }
    if (value.longValue() < 1) {
        value = value.add(BigDecimal.ONE);
    }
    BigDecimal trailed = value.stripTrailingZeros();
    int precision = trailed.scale();
    if (precision > MAXIMUM_PRECISION) {
        throw new KarakuRuntimeException(
                String.format("Attribute '%s' of bean '%s' has a precision of {%d}, maximum allowed is {%d}",
                        field.getName(), bean.getClass().getSimpleName(), precision, MAXIMUM_PRECISION));
    }
}

From source file:org.thymeleaf.util.EvaluationUtilTest.java

@Test
public void convertToBooleanTest() {

    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean(null));

    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(Boolean.TRUE));
    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean(Boolean.FALSE));

    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean(BigDecimal.ZERO));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(BigDecimal.ONE));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(BigDecimal.TEN));

    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean(BigInteger.ZERO));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(BigInteger.ONE));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(BigInteger.TEN));

    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean(Double.valueOf(0.0d)));
    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean(Float.valueOf(0.0f)));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(Double.valueOf(0.1d)));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(Float.valueOf(0.1f)));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(Double.valueOf(-0.1d)));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(Float.valueOf(-0.1f)));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(Double.valueOf(Double.MAX_VALUE)));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(Float.valueOf(Float.MAX_VALUE)));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(Double.valueOf(Double.MIN_VALUE)));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(Float.valueOf(Float.MIN_VALUE)));

    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean(Character.valueOf((char) 0)));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(Character.valueOf('x')));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(Character.valueOf('0')));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(Character.valueOf('1')));

    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean("true"));
    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean("false"));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean("yes"));
    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean("no"));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean("on"));
    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean("off"));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean("sky"));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean("high above"));

    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(new LiteralValue("true")));
    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean(new LiteralValue("false")));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(new LiteralValue("yes")));
    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean(new LiteralValue("no")));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(new LiteralValue("on")));
    Assert.assertFalse(EvaluationUtil.evaluateAsBoolean(new LiteralValue("off")));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(new LiteralValue("sky")));
    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(new LiteralValue("high above")));

    Assert.assertTrue(EvaluationUtil.evaluateAsBoolean(EvaluationUtil.class));

}