Java BigDecimal Round roundPrice(BigDecimal price, BigDecimal ticksize)

Here you can find the source of roundPrice(BigDecimal price, BigDecimal ticksize)

Description

Rounds the specified price to the specified tick size

License

Open Source License

Parameter

Parameter Description
price The price to round
ticksize The ticksize to round to

Return

A properly formatted BigDecimal for the given price and tick size.

Declaration

public static BigDecimal roundPrice(BigDecimal price, BigDecimal ticksize) 

Method Source Code

//package com.java2s;
/**// www .j a v  a  2  s.  c o m
 * MIT License
    
Copyright (c) 2015  Rob Terpilowski
    
Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
and associated documentation files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.math.BigDecimal;

public class Main {
    /**
     * Rounds the specified price to the specified tick size
     * @param price The price to round
     * @param ticksize The ticksize to round to
     * @return A properly formatted BigDecimal for the given price and tick size.
     */
    public static BigDecimal roundPrice(BigDecimal price, BigDecimal ticksize) {
        return roundPrice(price, ticksize, ticksize.scale());
    }

    /**
     * Rounds the price to the specified tick size and number of decimal places
     * 
     * @param price The price to round
     * @param ticksize The tick size to round to
     * @param decimalPlaces The number of decimal places to round to.
     * @return A properly formatted BigDecimal for the given tick size and decimal places.
     */
    public static BigDecimal roundPrice(BigDecimal price, BigDecimal ticksize, int decimalPlaces) {
        int intValue = price.intValue();
        double fraction = price.doubleValue() - intValue;
        long ticks = Math.round(fraction / ticksize.doubleValue());
        BigDecimal returnValue = new BigDecimal(intValue).add(new BigDecimal(ticks).multiply(ticksize));
        returnValue.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
        return returnValue;
    }
}

Related

  1. rounded(BigDecimal amount)
  2. roundHalfUp2Scale(BigDecimal value)
  3. roundImpl(final BigDecimal number, final int minFractionDigits, final int maxFractionDigits, final RoundingMode mode)
  4. roundingError(final BigDecimal dividend, final BigDecimal divisor, final int roundingMode)
  5. roundOff(final BigDecimal amount)
  6. roundTo(final BigDecimal bd, final int numberOfDecPlaces, final int finalScale)
  7. roundToMSDecimal(BigDecimal sourceDecimal)
  8. roundUp1(BigDecimal number)
  9. valueOfBigDecimalRounding(double d1, int decimalPrecision)