Java 8 BigDecimal - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 BigDecimal class.

This code example shows how to use the following methods:
public class JdbcDecimalTest 
{ 
    @Test 
    public void testComposeDecompose() 
    { 
        BigDecimal expected = new BigDecimal("123456789123456789.987654321");
        DecimalSerializer decimal = new DecimalSerializer();
         
        ByteBuffer buffer = decimal.serialize(expected);
        BigDecimal actual = decimal.deserialize(buffer);
        Assert.assertEquals(expected, actual);
    } 
} 
8
CodeRank
This code example shows how to use the following methods:
 
        BigDecimal decimal;
 
        try 
        { 
            decimal = new BigDecimal(source);
        } 
        catch (Exception e)
        { 
            throw new MarshalException(String.format("unable to make BigDecimal from '%s'", source), e);
        } 
 
        return decompose(decimal);
    } 
 
    public CQL3Type asCQL3Type() 
    { 
        return CQL3Type.Native.DECIMAL; 
    } 
 
8
CodeRank
This code example shows how to use the following methods:toString
     * when value is not a valid representation of BigDecimal. 
     */ 
    public void testConstrStringException() { 
        String a = "-238768.787678287a+10";
        try { 
            BigDecimal bd = new BigDecimal(a);
            fail("NumberFormatException has not been caught: " + bd.toString());
        } catch (NumberFormatException e) {}
    } 
 
    /** 
     * new BigDecimal(String value, MathContext) 
     */ 
    public void testConstrStringMathContext() { 
        String a = "-238768787678287e214";
        int precision = 5;
        RoundingMode rm = RoundingMode.CEILING;
        MathContext mc = new MathContext(precision, rm);
        String res = "-23876";
        int resScale = -224;
8
CodeRank
This code example shows how to use the following methods:
    public final static boolean compareTo(Object object, Object attribute, String key, NSDictionary params){
        if(attribute instanceof Number){
            String sign = (String)params.objectForKey("Operator");
            String number = (String)params.objectForKey("RightOperand");
            BigDecimal left = new BigDecimal(((Number)attribute).doubleValue());
            BigDecimal right = new BigDecimal(number);
            int comparisonValue=left.compareTo(right);
 
            if(sign.equals(GSVNumberMethods.EQUAL)){
                return(comparisonValue==0);
            } else if(sign.equals(GSVNumberMethods.NOT_EQUAL)){
                return(comparisonValue!=0);
            } else if(sign.equals(GSVNumberMethods.GREATER_THAN)){
                return(comparisonValue==1);
            } else if(sign.equals(GSVNumberMethods.GREATER_EQUAL)){
                return(comparisonValue==1 || comparisonValue==0);
            } else if(sign.equals(GSVNumberMethods.LESS_THAN)){
                return(comparisonValue==-1);
            } else if(sign.equals(GSVNumberMethods.LESS_EQUAL)){
                return(comparisonValue==-1 || comparisonValue==0);
7
CodeRank
This code example shows how to use the following methods:compareTo, precision, scale, setScale, stripTrailingZeros
    bd = normalize(bd, true);
    return bd == null ? null : new HiveDecimal(bd);
  } 
 
  public static HiveDecimal create(BigInteger bi) {
    BigDecimal bd = normalize(new BigDecimal(bi), true);
    return bd == null ? null : new HiveDecimal(bd);
  } 
 
  public static HiveDecimal create(int i) {
    return new HiveDecimal(new BigDecimal(i));
  } 
 
  public static HiveDecimal create(long l) {
    return new HiveDecimal(new BigDecimal(l));
  } 
 
  @Override 
  public String toString() {
     return bd.toPlainString();

7
CodeRank
This code example shows how to use the following methods:add, subtract
 
	public void testMoney() 
	{ 
		should("properly set, take, give, and get money"); 
		User user = ess.getUser(base1);
		BigDecimal i = new BigDecimal("100.5");
		try 
		{ 
			user.setMoney(i);
			user.takeMoney(new BigDecimal(50));
			i = i.subtract(BigDecimal.valueOf(50));
			user.giveMoney(new BigDecimal(25));
			i = i.add(BigDecimal.valueOf(25));
		} 
		catch (MaxMoneyException ex)
		{ 
			fail();
		} 
		 
		assertEquals(user.getMoney(), i);

7
CodeRank
This code example shows how to use the following methods:equals
		if (!(o instanceof MonetaryAmount)) return false;
 
		final MonetaryAmount monetaryAmount = (MonetaryAmount) o;
 
		if (!currency.equals(monetaryAmount.currency)) return false;
		if (!value.equals(monetaryAmount.value)) return false;
 
		return true; 
	} 
 
	public int hashCode() { 
		int result;
		result = value.hashCode();
		result = 29 * result + currency.hashCode();
		return result;
	} 
 
	public String toString() {
		return "Value: '" + getValue() + "', " + 
		        "Currency: '" + getCurrency() + "'"; 
7
CodeRank
This code example shows how to use the following methods:divide
        else if (r instanceof Double)
          return ((BigDecimal) l).divide(new BigDecimal(r.doubleValue()), RoundingMode.HALF_UP);
        else if (r instanceof Long)
          return ((BigDecimal) l).divide(new BigDecimal(r.longValue()), RoundingMode.HALF_UP);
        else if (r instanceof Integer)
          return ((BigDecimal) l).divide(new BigDecimal(r.intValue()), RoundingMode.HALF_UP);
        else if (r instanceof Short)
          return ((BigDecimal) l).divide(new BigDecimal(r.shortValue()), RoundingMode.HALF_UP);
      } 
    } 
 
    return null; 
  } 
 
  @Override 
  public OIndexReuseType getIndexReuseType(Object iLeft, Object iRight) {
    return OIndexReuseType.NO_INDEX; 
  } 
 
  @Override 
7
CodeRank
This code example shows how to use the following methods:divide, multiply, scale, setScale
	 * Override this in your subclass to provide for other operations when formatting a value. 
	 * @param value 
	 */ 
	protected BigDecimal performFormat(BigDecimal value) {
		if("*".equals(_operator)) {
			value = value.multiply(_factor);
		} else if("/".equals(_operator)) {
		    int scale = _scale == null ? value.scale() : _scale.intValue();
			value = value.divide(_factor, scale, BigDecimal.ROUND_HALF_EVEN);
		} 
		return value;
	} 
	 
	/** 
	 * Override this in your subclass to provide for other operations when parsing a value. 
	 * @param value 
	 */ 
	protected BigDecimal performParse(BigDecimal value) {
		if("*".equals(_operator)) {
		    int scale = _scale == null ? value.scale() : _scale.intValue();

7
CodeRank
This code example shows how to use the following methods:compareTo
    private static final boolean is1_7;
    private static final boolean is1_8;
 
 
    static { 
        BigDecimal v = new BigDecimal ( "-1" );
        String sversion = System.getProperty ( "java.version" );
        if ( sversion.indexOf ( "_" ) != -1 ) {
            final String[] split = sversion.split ( "_" );
            try { 
 
                String ver = split [0];
                if (ver.startsWith ( "1.8" )) {
                    v = new BigDecimal ("1.8" );
                } 
                if (ver.startsWith ( "1.7" )) {
                    v = new BigDecimal ("1.7" );
                } 
 
                if (ver.startsWith ( "1.6" )) {

7
CodeRank

Related Java 8 BigDecimal Questions & Answers:

See Code Examples for Java 8 BigDecimal Methods: