var clipboard = new ClipboardJS('.btn-copy');
var xelem = document.getElementById("d");
var sel1elem = document.getElementById("from");
var sel2elem = document.getElementById("to");
var yelem = document.getElementById("y");
var calgroup = document.getElementById("calgroup");
var cal1elem = document.getElementById("cal1");
var cal2elem = document.getElementById("cal2");
window.addEventListener("DOMContentLoaded", function () {
calgroup.style.display = "none";
var params = GetURLParams();
/*
if (Object.keys(params).length > 0 && params.d != "") {
xelem.value = params.d;
sel1elem.selectedIndex = params.from - 2;
sel2elem.selectedIndex = params.to - 2;
onconvert()
}
*/
params = JSON.parse(JSON.stringify(params))
if (Object.keys(params).length > 0) {
console.log('params', params)
if (params.d) {
xelem.value = params.d;
}
if (params.from) {
sel1elem.selectedIndex = params.from - 2;
}
if (params.to) {
sel2elem.selectedIndex = params.to - 2;
}
if (params.d && params.from && params.to) {
onconvert()
}
}
});
function GetURLParams() {
var url = window.location.href;
var regex = /[?&]([^=#]+)=([^]*)/g,
params = {},
match;
while (match = regex.exec(url)) {
params[match[1]] = match[2]
}
return params
}
function swap() {
var i1 = sel1elem.selectedIndex;
var i2 = sel2elem.selectedIndex;
sel1elem.selectedIndex = i2;
sel2elem.selectedIndex = i1;
xelem.value = yelem.value;
yelem.value = ""
}
function base2decimal(x, dec, b) {
var txt = "(" + x + ")" + b.toString() + " = ";
var d, e, minus = false;
var len = x.length;
if (x[0] == "-") {
txt += "-[";
x = x.substr(1);
len--;
minus = true
}
var idot = x.indexOf(".");
if (idot >= 0) {
x = x.substring(0, idot) + x.substring(idot + 1, len);
len--
} else idot = len;
for (var i = 0; i < len; i++) {
d = parseInt(x[i], b);
e = idot - i - 1;
e = e.toString();
txt += "(" + d + " × " + b + "" + e + ")";
if (i < len - 1) txt += " + "
}
if (minus) txt += "]";
txt += " = (" + dec + ")10";
document.getElementById("cal1txt").innerHTML = txt
}
function digits_after_period(x) {
f = x.toString();
i = f.indexOf('.');
len = f.length - i - 1;
return len
}
function decimal2base(dec, y, b) {
var row, txt = "";
document.getElementById("cal2tbl").getElementsByTagName('tbody')[0].innerHTML = "";
if (dec < 0) dec = -dec;
dec = dec.toString();
var id = dec.indexOf(".");
var nd = y.length - y.indexOf(".") - 1;
if (nd > 6) nd = 6;
if (id >= 0 && nd > 0) {
txt = "将目标基数乘以结果的小数位数(最高可达6位数):
";
txt += "floor(" + dec + "×" + b + "" + nd + ") = ";
dec = Math.floor(dec * Math.pow(b, nd));
txt += dec
}
document.getElementById("cal2dec").innerHTML = txt;
for (var n = dec, k = 0; n >= 1; k++) {
row = "