Rendondeo a decena 1
Prueba de redondeo a decenas con división y multiplicación. Poco óptima.
HTML
<h2>
Method 1. Round dividing
</h2>
<table>
<tr id="data">
<th>Results</th>
</tr>
</table>
<p>
<a href="https://jsfiddle.net/juglan/h7qyfbtg/">Method 2</a> |
<a href="https://jsfiddle.net/juglan/u8ss5n16/">Method 3</a>
</p>
CSS
* {
font-family: Tahoma, Arial, Verdana;
}
td,
th {
border: 1px solid #666;
padding: 4px 8px;
}
th {
background-color: #E0E0E0;
}
table {
border-collapse: collapse;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
h2 {
font-size: 18px;
}
JavaScript
function round(value) {
return parseInt(value / 10) * 10;
}
function addResult(value, row, last) {
var node = document.createElement(last ? 'th' : 'td'),
textnode = document.createTextNode(value);
node.appendChild(textnode);
row.appendChild(node);
}
function test() {
var roundTime = 0,
row = document.getElementById('data'),
lap, startTime, i, endTime, currentResult;
for (lap = 0; lap < 10; ++lap) {
startTime = new Date();
for (i = 0; i < 10000000; ++i) {
round(-5000000 + i);
}
endTime = new Date();
currentResult = endTime.getTime() - startTime.getTime()
roundTime += currentResult;
addResult(currentResult, row, false);
}
addResult((roundTime / 10) + 'ms', row, true);
}
test();