如何在javascript中显示90°的风向(包括使用数字后面的小圆圈度数符号)?
我只能在文字描述中显示 - 下面的代码。
谢谢。
function text(d) {
let directions = ['Northerly', 'North Easterly', 'Easterly', 'South Easterly', 'Southerly', 'South Westerly', 'Westerly', 'North Westerly'];
d += 22.5;
if (d < 0)
d = 360 - Math.abs(d) % 360;
else
d = d % 360;
let w = parseInt(d / 45);
return `${directions[w]}`;
}
最佳答案
非常简单。只需检查 utf16 字符表并使用 String.fromChatCode() 即可。这里:
function text(d) {
let directions = ['Northerly', 'North Easterly', 'Easterly', 'South Easterly', 'Southerly', 'South Westerly', 'Westerly', 'North Westerly'];
d += 22.5;
if (d < 0)
d = 360 - Math.abs(d) % 360;
else
d = d % 360;
let w = parseInt(d / 45);
return `${directions[w]}`;
}
deg = 67;
console.log(deg + String.fromCharCode(0xfeff00b0) + ' = ' + text(deg));
这是您可以找到 UTF16 表的众多位置之一:
http://www.fileformat.info/info/charset/UTF-16/list.htm 我还纠正了您的代码中的错误。请注意,“度”变量不存在。应该是“d”
这是您的函数的更高级版本,供您研究一下:
dirToStr = (d) => {
const directions = ['Northerly', 'North Easterly', 'Easterly', 'South Easterly', 'Southerly', 'South Westerly', 'Westerly', 'North Westerly'];
d = d < 0 ?
d = 360 - Math.abs(d) % 360
: d % 360;
return `${directions[d / 45 | 0]}`;
}
const degreeChar = String.fromCharCode(0xfeff00b0);
deg = 67;
console.log(`${deg}${degreeChar} = ${dirToStr(deg)}`);
关于javascript - 在javascript中以度数显示风向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61355975/