Showing posts with label Rounding Nearest. Show all posts
Showing posts with label Rounding Nearest. Show all posts

Saturday, December 13, 2014

jQuery rounding number to nearest, flooring or ceiling as expected decimal point


function roundNumber(value, exp) {
    return decimalAdjust('round', value, exp);
}

function floorNumber(value, exp) {
    return decimalAdjust('floor', value, exp);
}

function ceilNumber(value, exp) {
    return decimalAdjust('ceil', value, exp);
}

function decimalAdjust(type, value, exp) {
    // If the exp is undefined or zero...
    if (typeof exp === 'undefined' || +exp === 0) {
        return Math[type](value);
    }
    value = +value;
    exp = +exp;
    // If the value is not a number or the exp is not an integer...
    if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
        return NaN;
    }
    // Shift
    value = value.toString().split('e');
    value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
    // Shift back
    value = value.toString().split('e');
    return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}

jQuery Rounding Number To Nearest Interval Such: 0.05, 0.5, 0.1, 5, 10, 100

JSFIDDLE LINK ||| TEXT LINK

Html Content:
<input type='text' name='number' style='width: 150px;' value='10.21'/>
<input type='text' name='round' style='width: 50px;' value='0.25'/>
<input type='text' name='dp' style='width: 50px;' value='2'/>
<input type='button' name='check' value='Check'/>
<div></div>

jQuery Content:


var div = $("div");
log("---------------------------------------------------------");

$("input[type='button']").click(function() {
    var number = parseFloat($("input[name='number']").val());
    var round = parseFloat($("input[name='round']").val());
    var dp = parseFloat($("input[name='dp']").val());
    if (!isNaN(number) && !isNaN(round) && !isNaN(dp)) {
        var nn = roundInterval(number, round, dp, "up");
        
        log("<br/>Rounding: " + numberToString(number, dp, true) + 
            " using: " + round + 
            "/" + "<span class='up-down'>Up</span>" + 
            " as: " + numberToString(nn, dp, true));
        
        var nn = roundInterval(number, round, dp, "down");
        
        log("<br/>Rounding: " + numberToString(number, dp, true) + 
            " using: " + round + 
            "/" + "<span class='up-down'>Down</span>" + 
            " as: " + numberToString(nn, dp, true));
        
        log("---------------------------------------------------------");
    }
});
$("input[type='button']").trigger("click");

function roundInterval(number, interval, round, roundType) {
    if (round > 9) {
        round = 9;
    }
    else if (round < 0) {
        round = 0;
    }
    number = number > 999999999999999 ? 999999999999999 : number;
    var isMinus = false;
    if (number < 0) {
        isMinus = true;
        number = number * -1;
    }
    number = parseFloat(numberToString(number, round));
    interval = parseFloat(numberToString(interval, round));
    var multiplier = roundType == 'up' ? Math.ceil(number / interval) : Math.floor(number / interval);
    
    log("Multiplier: " + multiplier);
    log("As: " + number + "/" + interval + " :: " + (number/interval));
    
    number = multiplier * interval;
    
    number = multiplier * interval;
    if (isMinus) {
        number = number * -1;
    }
    return parseFloat(number.toFixed(round));
}

function numberToString(number, dp, asDp) {
    var format = '#';
    if (asDp === undefined || asDp == null) {
        asDp = false;
    }
    if (dp !== undefined && dp != null && !isNaN(dp)) {
        if (dp > 9) {
            dp = 9;
        }
    }
    else {
        dp = 0;
    }
    if (dp > 0 && asDp) {
        format += ".";
        format += "0000000000".substr(0, dp);
    }
    else if (dp > 0) {
        format += ".";
        format += "##########".substr(0, dp);
    }
    number = number.toString();
    var minus = number.substr(0, 1) == '-' ? '-' : '';
    var ln = "";
    if (number.lastIndexOf("e+") > -1) {
        ln = number.substr(0, number.lastIndexOf("e+"));
        for (var i = ln.length - 2; i < parseInt(number.substr(number.lastIndexOf("e+") + 1)); i++) {
            ln += "0";
        }
        ln = ln.replace(/[^0-9]/g, '');
        number = ln;
    }
    var tail = format.lastIndexOf('.'), nail = number.lastIndexOf('.');
    if (nail < 0 && tail >= 0) {
        number = number + "." + format.substr(tail + 1, 1);
        nail = number.lastIndexOf('.');
    }
    tail = tail > -1 && nail > -1 ? format.substr(tail) : '';
    var numtail = number.substr(number.indexOf(".") ) ;
    if(tail.length > 0 && dp !== undefined && dp > 0) {
        tail = tail.substr(0, dp + 1);
        var tails = tail.split(''), ntail = "", canPop = true;
        for (var i = 1; i < tails.length; i++) {
            if ((tails[i] == '#' || tails[i].match(/([0-9])/g)) && numtail.length > i) {
                ntail += numtail.substr(i, 1);                    
            }
            else if(tails[i] == '#') {
                ntail += '0';
            }
            else {
                ntail += tails[i];
            }
        }
        var ttail = ntail.split(''), ptail = tail.substr(1).split('');
        for(var i = ttail.length - 1; i > -1; i--){
            if (ptail[i] == '#' && canPop && (ttail[i] == '0' || ttail[i] == '#')) {
                ntail = ntail.substr(0, ntail.length - 1);
            }
            else {
                canPop = false;
            }
        }
        if (ntail.length > 0) {
            tail = "." + ntail;
        }
        else {
            tail = "";   
        }
    }
    number = number.replace(/\..*|[^0-9]/g,'').split('');
    format = format.replace(/\..*/g,'').split('');
    for(var i = format.length - 1; i > -1; i--){
        if(format[i] == '#') {
            format[i]=number.pop()
        }
    }
    number = minus + number.join('') + format.join('') + tail;
    return number;
}

function log(m) {
    div.prepend(m + "<br/>");
}

Thursday, June 5, 2014

Java: Rounding Number To Nearest Interval Such: 0.05, 0.5, 0.1, 5, 10, 100

RoundNumber.java


import java.math.BigDecimal;
import java.math.RoundingMode;
class RoundNumber {
    public static void main(String[] args) throws Exception {
         BigDecimal Price = new BigDecimal("232.573");
         BigDecimal Tick = new BigDecimal("0.05");
         BigDecimal Price2 = Price.divide(Tick, 9, RoundingMode.HALF_EVEN);
         Price2 = Price2.setScale(0, RoundingMode.HALF_UP).multiply(Tick);
         System.out.println("Price: " + Price + ", Round: " + Tick + ", Converted: " + Price2);
    }
}

Output:


Price: 232.573, Round: 0.05, Converted: 232.55
Price: 232.533, Round: 0.05, Converted: 232.55
Price: 232.523, Round: 0.05, Converted: 232.50
Price: 232.5235455, Round: 0.005, Converted: 232.525
Price: 232.5235, Round: 0.5, Converted: 232.5
Price: 232.446, Round: 0.5, Converted: 232.5
Price: 232.246, Round: 0.5, Converted: 232.0
Price: 1455.546, Round: 0.05, Converted: 1455.55
Price: 1455.528, Round: 0.05, Converted: 1455.55
Price: 1455.524, Round: 0.05, Converted: 1455.50
Price: 232.246, Round: 5, Converted: 230
Price: 232.546, Round: 5, Converted: 235
Price: 232.546, Round: 10, Converted: 230
Price: 235.546, Round: 10, Converted: 240
Price: 35.546, Round: 100, Converted: 0
Price: 55.546, Round: 100, Converted: 100
Price: 1455.524, Round: 100, Converted: 1500
Price: 1355.524, Round: 100, Converted: 1400
Price: 1248.524, Round: 100, Converted: 1200
Price: 1455.546, Round: 0.10, Converted: 1455.50
Price: 1455.556, Round: 0.10, Converted: 1455.60

External Link