Examples of Decimal

Decimal objects are immutable. Many operations return new Decimal objects.

Currency Is Unspecified

This class can be used to model amounts of money.

Many will be surprised that this class does not make any reference to currency. The reason for this is adding currency would render this class a poor building block. Building block objects such as Date, Integer, and so on, are atomic, in the sense of representing a single piece of data. They correspond to a single column in a table, or a single form control. If the currency were included in this class, then it would no longer be atomic, and it could not be treated by WEB4J as any other building block class. However, allowing this class to be treated like any other building block class is highly advantageous.

If a feature needs to explicitly distinguish between multiple currencies such as US Dollars and Japanese Yen, then a Decimal object will need to be paired by the caller with a second item representing the underlying currency (perhaps modeled as an Id). See the {@link Currency} class for more information.

Number of Decimal Places

To validate the number of decimals in your Model Objects, call the {@link Check#numDecimalsAlways(int)} or {@link Check#numDecimalsMax(int)} methods.

The {@link #init(RoundingMode,int)} method is called upon startup.It takes a parameter which specifies the number of decimal places. It is used only for rounding the results of times and div operations. It is not used to validate the number of decimals in items passed to the Decimal constructor.

Different Numbers of Decimals

As usual, operations can be performed on two items having a different number of decimal places. For example, these operations are valid (using an informal, ad hoc notation) :

10 + 1.23 = 11.23 10.00 + 1.23 = 11.23 10 - 1.23 = 8.77 (10 > 1.23) => true 
This corresponds to typical user expectations.

Note that {@link #equals(Object)} is unusual in that it is the only method sensitive to the exact number of decimal places, while {@link #eq(Decimal)} is not. That is,

10.equals(10.00) => false 10.eq(10.00) => true

Results With 'Extra' Decimal Places

The times and div operations are different, since the result can have a larger number of decimals than usual. For example, when dealing with US Dollars, the result

$10.00 x 0.1256 = $1.256
has more than two decimals. In such cases, this class will round results of multiplication and division, using the setting passed to {@link #init(RoundingMode,int)}. This policy likely conforms to the expectations of most end users. The {@link #times(long)} method is an exception to this rule.

The {@link #init(RoundingMode,int)} method takes two parameters.One controls the rounding policy, and the other controls the number of decimals to use by default. When the default number of decimals needs to be overridden, you must use the {@link #changeTimesDivDecimals(int)} method.

Terse Method Names

Various methods in this class have unusually terse names, such as lt for 'less than', and gt for 'greater than', and so on. The intent of such names is to improve the legibility of mathematical expressions.

Example :

 if ( amount.lt(hundred) ) { cost = amount.times(price);  }

Prefer Decimal to Double

The times and div methods are overloaded to take int for round numbers, and Decimal or double for numbers with a decimal.

In short, the double versions are best suited when using hard-coded values for factors and divisors, while the Decimal versions are suited for the (more common) case of using values coming from the database or user input.

Using Decimal is the preferred form, since there are many pitfalls associated with double. The double form has been retained since it's more convenient for the caller in some cases, and one of the goals of this class is to allow terse mathematical expressions.

Extends Number

This class extends {@link Number}. An immediate benefit of this is that it allows JSTL's fmt tags to render Decimal objects in the usual way.
  • net.sf.cb2java.types.Decimal
    class that represents decimal (zoned) data types @author James Watson
  • org.apache.hadoop.hive.metastore.api.Decimal
  • org.apache.sqoop.schema.type.Decimal
    Fixed point number with configurable precision and scale. JDBC Types: numeric, decimal
  • org.openfeed.InstrumentDefinition.Decimal
  • org.openfeed.proto.inst.Decimal

  • Examples of cli.System.Decimal

                }
            } else if (targetType == CLI_DECIMAL) {
                try {
                    AtomicValue v = getAtomicValue(value, targetType, context);
                    if (v instanceof DoubleValue) {
                        return new Decimal(((DoubleValue) v).getDoubleValue());
                    } else if (v instanceof FloatValue) {
                        return new Decimal(((FloatValue) v).getFloatValue());
                    } else if (v instanceof Int64Value) {
                        return new Decimal(((Int64Value) v).longValue());
                    } else if (v instanceof BigIntegerValue) {
                        return Decimal.Parse(v.getStringValue());
                    } else if (v instanceof DecimalValue) {
                        return Decimal.Parse(v.getStringValue());
                    } else {
    View Full Code Here

    Examples of com.barchart.util.value.api.Decimal

      }

      @Override
      public final boolean equals(final Object thatValue) {
        if (thatValue instanceof Decimal) {
          final Decimal that = (Decimal) thatValue;
          return this.compareTo(that) == 0;
        }
        return false;
      }
    View Full Code Here

    Examples of com.barchart.util.value.api.Decimal

      }

      @Override
      public final boolean equals(final Object thatValue) {
        if (thatValue instanceof Decimal) {
          final Decimal that = (Decimal) thatValue;
          return this.compareTo(that) == 0;
        }
        return false;
      }
    View Full Code Here

    Examples of com.barchart.util.value.api.Decimal

      // }

      @Override
      public final boolean equals(final Object thatValue) {
        if (thatValue instanceof Decimal) {
          final Decimal that = (Decimal) thatValue;
          return this.compareTo(that) == 0;
        }
        return false;
      }
    View Full Code Here

    Examples of com.bleujin.framework.valid.validator.Decimal

       
      }
     
      public void testDecimal() throws Exception {
        b.setSvalue("123") ;
        assertEquals(true, new Decimal(b, SVALIE).isValid()) ;
       
        b.setSvalue("-123") ;
        assertEquals(true, new Decimal(b, SVALIE).isValid()) ;

        b.setSvalue("-123.12") ;
        assertEquals(true, new Decimal(b, SVALIE).isValid()) ;
       
        b.setSvalue("-123.") ;
        assertEquals(true, new Decimal(b, SVALIE).isValid()) ;

        b.setSvalue("+12.3.") ;
        assertEquals(false, new Decimal(b, SVALIE).isValid()) ;

      }
    View Full Code Here

    Examples of com.confluenity.jaylen.entity.math.Decimal

      private long trackId;
      private long id;

      public Particle(int id, String x, String y, String z) {
        this.id = id;
        this.r.set(X, new Decimal(x));
        this.r.set(Y, new Decimal(y));
        this.r.set(Z, new Decimal(z));
        this.v.set(X, new Decimal());
        this.v.set(Y, new Decimal());
        this.v.set(Z, new Decimal());
        this.snapshotId = VOID_SNAPSHOT_ID;
        this.trackId = VOID_TRACK_ID;
      }
    View Full Code Here

    Examples of com.confluenity.jaylen.entity.math.Decimal

      public boolean isAssignedToSnapshot() {
        return snapshotId >= 0 && snapshotId != VOID_SNAPSHOT_ID;
      }

      public Decimal getDistance(final Particle other) {
        Decimal squareDistance = new Decimal();
        for (int j = 0; j < DIMENSIONS; j++) {
          squareDistance.add(getX().sub(other.getX()).sqr());
        }
        return squareDistance.sqrt();
      }
    View Full Code Here

    Examples of com.confluenity.jaylen.entity.math.Decimal

        }
        return squareDistance.sqrt();
      }

      public Decimal getVelocity() {
        Decimal velocityModule = new Decimal();
        for (int j = 0; j < DIMENSIONS; j++) {
          velocityModule.add(getVx().sqr());
        }
        return velocityModule.sqrt();
      }
    View Full Code Here

    Examples of hirondelle.web4j.model.Decimal

        else if ( aObject instanceof BigDecimal ){
          BigDecimal amount = (BigDecimal)aObject;
          result = renderBigDecimal(amount);
        }
        else if ( aObject instanceof Decimal ){
          Decimal money = (Decimal)aObject;
          result = renderBigDecimal(money.getAmount());
        }
        else if ( aObject instanceof TimeZone ) {
          TimeZone timeZone = (TimeZone)aObject;
          result = timeZone.getID();
        }
    View Full Code Here

    Examples of hirondelle.web4j.model.Decimal

        else if ( aObject instanceof BigDecimal ){
          BigDecimal amount = (BigDecimal)aObject;
          result = getBigDecimalDisplayFormat().format(amount.doubleValue());
        }
        else if ( aObject instanceof Decimal ){
          Decimal money = (Decimal)aObject;
          result = getBigDecimalDisplayFormat().format(money.getAmount().doubleValue());
        }
        else if ( aObject instanceof Boolean ){
          Boolean value = (Boolean)aObject;
          result = getBooleanDisplayText(value);
        }
    View Full Code Here
    TOP
    Copyright © 2018 www.massapi.com. All rights reserved.
    All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.