var dec = prompt("Enter a number");
if (dec != null && dec > 0) {
var hex = dec.toString(16);
alert(hex);
}
else {
alert("number must be greater than 0");
}
我有以下代码当我在提示中输入一个号码时,它会提醒相同的号码示例:我输入256警报:256
prompt
将返回一个string
。只需先将其转换为普通的number
即可。
var dec = +prompt("Enter a number");
if (dec != null && dec > 0) {
var hex = dec.toString(16);
alert(hex);
}
else {
alert("number must be greater than 0");
}
试试这个
var dec = prompt("Enter a number");
if (dec != null && dec > 0) {
var hex = parseInt(dec,16);
alert(hex);
}
else {
alert("number must be greater than 0");
}