I like your solution (in the end, I would also just use Math.round()
). Here is my suggestion (a little bit shorter, but the same code):
function roundDblDigitHex(x) {
x = Math.min(Math.max(Math.round(x), 0), 255);
return ("0" + x.toString(16)).slice(-2);
}