Page 1 of 1

what went wrong?

Posted: Fri Oct 21, 2005 12:16 pm
by J_Iceman05
I'm trying to make a number round up if it goes too many spaces past a decimal point.
Example: 7895431531579543114684 = ok
6548.65154684897456167489156 = (if spaces = 10, number should be 6548.6515468490)

That works. But, if only if the number has decimal places that surpass the give allowed amount.
if i just use a whole number, nothing happens. no value is returned or anything
if the allowed spaces = 5, but the number has 4 numbers past the decimal point, still nothing happens.

Code: Select all

function NumRound (number,spaces) {
    // if the number is a whole number, simply return that number
    if (number.split('.')){
        var num = number.split('.');
    } else {
        return number;
    }
  
    // check of the length of the decimal places is longer than allowed
    if (num[1].length > spaces) {
        var count = 0;
        var decCount = 1;
        while (count < spaces) {
            decCount *= 10;
            count++;
        }
        // makes the number of spaces past decimal point become part of whole number
        number *= decCount;

        // round it to remove excess numbers
        number = Math.round(number);

        // turn back into original whole number
        number /= decCount;
        alert(number.length);                           // debugging
        alert(num[1].length+' > '+spaces);            // debugging
        alert(number+"\n"+num[1]);                    // debugging
    }

    return number;
}