[英]Convert degrees value to units from 0 to 100
我有一個向我返回0到360度之間的值的組件,我需要將該值傳遞給單位,例如0到100。
我有以下功能,但看起來很丑
function degToValue( deg )
{
var unit = Math.ceil( ( deg * 100 ) / 360 );
if ( unit === 0 ) unit = 100;
if ( deg < 1.6 && deg > 0 ) unit = 0;
return unit;
}
您知道更優雅的方式嗎?
您可以將其除以3.6
並使用模數使它更漂亮:
function degToValue( deg )
{
var unit = (deg / 3.6) % 100;
return (unit === 0 ? 100 : unit);
}
console.log( degToValue( 360 )); //100
console.log( degToValue( 0 )); //100
console.log( degToValue( 355 )); //98.61
console.log( degToValue( 719 )); //99.72
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.