round beautiful

by grabantot

HTML

<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<input id="in" type="text" value="-0.0018">
<br><input type="button" value="bautify">
<br><input id="out" type="text">
<br><textarea rows="30" cols="50"></textarea>

JavaScript

var ceilAbs = function(num, to, bias) {
	if (!to) to = [-2, -5, -10]
	if (!bias) bias = 0
  var multiples = to.filter(function(value) {return value > 0})
  to = to.filter(function(value) {return value < 0}).map(Math.abs)
  
  var numAbs = Math.abs(num) - bias
  var exponent = Math.floor( Math.log10(numAbs) )
  var scientific = Math.abs(numAbs) * Math.pow(10, -exponent)
  var roundedSc = Infinity
  console.log(multiples)
  console.log(to)
  console.log(numAbs)
  console.log(exponent)
  console.log(scientific)
  console.log(roundedSc)
  for (var i=0; i<multiples.length; i++) {
  	var candidate = multiples[i] * Math.ceil(scientific / multiples[i])
  	console.log('candidate: ' + candidate)
    if (candidate >= scientific && candidate < roundedSc) {
    	roundedSc = candidate
      console.log('new rounded: ' + roundedSc)
    }
  }
  for (var i=0; i<to.length; i++) {
    console.log('to[i]: ' + to[i])
  	if (to[i] >= scientific && to[i] < roundedSc) {
    	roundedSc = to[i]
      console.log('new rounded: ' + roundedSc)
    }
  }
  var rounded = Math.sign(num) * roundedSc * Math.pow(10, exponent) + bias
  console.log('rounded: ' + rounded)
  return rounded
}

var round = function(num, to, bias) {
	if (!to) to = [-2.5, -5, -10, 0.2]
	if (!bias) bias = 0
  var multiples = to.filter(function(value) {return value > 0})
  to = to.filter(function(value) {return value < 0}).map(Math.abs)
  
  var numAbs = Math.abs(num) - bias
  var exponent = Math.floor( Math.log10(numAbs) )
  var scientific = Math.abs(numAbs) * Math.pow(10, -exponent)
  var roundedSc = Infinity
  console.log(multiples)
  console.log(to)
  console.log(numAbs)
  console.log(exponent)
  console.log(scientific)
  console.log(roundedSc)
  for (var i=0; i<multiples.length; i++) {
  	var candidate = multiples[i] * Math.round(scientific / multiples[i])
  	console.log('candidate: ' + candidate)
    if (Math.abs(candidate-scientific) < Math.abs(roundedSc-scientific) ) {
    	roundedSc = candidate
     ...