function trunc (x, decimalDigits = 0, returnText = true, longFixed = true) {
/*
decimalDigits = [0..15]
returnText = true ->return is a string
false -> return is a number
longFixed = true -> include non-significant zeros in the return string.
false -> NOT include non-significant zeros in the return string.
*If returned is a number, the non-significant zero does not make mathematical sense.
decimalDigits = [0..15]
returnText = true -> devuelve una cadena de texto
false -> devuelve un número
longFixed = true -> si la salida es una cadena de texto incluye
los ceros no significativos necesarios para
completar la cantidad decimalDigits
false -> no se incluyen ceros no significativos
*Si lo que se retorna es un número es indiferente el
valor de este parámetro por no tener sentido matemático.
*/
x = Number(x);
decimalDigits = Number(decimalDigits);
x = x * (10 ** decimalDigits);
x = x - x % 1;
x = x / (10 ** decimalDigits);
if(returnText)
if(longFixed)
return x.toFixed(decimalDigits);
else
return x.toString();
else
return x;
}
console.log('trunc(x = 0.4444, decimalDigits = 2, ouputText= true, longFixed = false) = ' +
trunc(0.4444, 2, true, false) + '\n');
console.log('trunc(x = -0.4444, decimalDigits = 2, ouputText= true, longFixed = true) = ' +
trunc(-0.4444, 2, true, true) + '\n');
console.log('trunc(x = 0, decimalDigits = 2, ouputText= true, longFixed = false) = ' +
trunc(0, 2, true, false) + '\n');
console.log('trunc(x = -0, decimalDigits = 2, ouputText= true, longFixed = true) = ' +
trunc(-0, 2, true, true) + '\n');
console.log('trunc(x = 1.4444, decimalDigits = 3, ouputText= false, longFixed = true) = ' +
trunc(1.4444, 3, false, true) + '\n');
console.log('trunc(x = 1.6666, decimalDigits = 3, ouputText= false, longFixed = false) = ' +
trunc(-1.6666, 3, false, false) + '\n');
console.log('trunc(x = 1.4444, decimalDigits = 8, ouputText= false, longFixed = true) = ' +
trunc(1.4444, 8, false, true) + '\n');
console.log('trunc(x = 1.6666, decimalDigits = 8, ouputText= false, longFixed = false) = ' +
trunc(-1.6666, 8, false, false) + '\n');
console.log('trunc(x = 1.4444, decimalDigits = 8, ouputText= true, longFixed = true) = ' +
trunc(1.4444, 8, true, true) + '\n');
console.log('trunc(x = 1.6666, decimalDigits = 8, ouputText= true, longFixed = false) = ' +
trunc(-1.6666, 8, true, false) + '\n');