我想比較 2 個值,根據這些值獲得我的圖例的顏色。但不幸的是,我不能使用 ceil、floor 或舍入這些值,因為它會影響我的結果。
我有以下顏色表,第一個值作為點,其余 3 個值是 rgb 值。
基本上我從后端得到了點,它可以是 0.4607441262895224、0.5500956769649571 等等。我需要將點與顏色表中的第一個值進行比較并給出相應的顏色。但是我在比較時遇到了問題,因為目前我已經給了 Fixed(2) 但不建議這樣做。
"colors": [
[ 0.00, 255, 13, 186 ],
[ 0.25, 254, 4, 135 ],
[ 0.50, 73, 255, 35 ],
[ 0.75, 185, 116, 255 ],
[ 1.00, 32, 50, 255 ]
]
// main logic
const test = arraySet.find((ele) => {
// point is dynamic
const point = 0.388920938
// I cannot use toFixed(2) here, which need to be changed
// ele[0] is the first value in array 0.00, 0.25, 0.50 and so on
return point.toFixed(2) == ele[0].toFixed(2);}
);
uj5u.com熱心網友回復:
我想你想要一些關于
Math.abs(point-ele[0]) < 0.01
替換為0.01與您的資料相關的閾值,例如,1e-6對于 10^(-6)
uj5u.com熱心網友回復:
ToFixed 也將被四舍五入
const toFixed=(num)=>Number(num.toString().match(/^\d (?:\.\d{0,2})?/));
uj5u.com熱心網友回復:
嘗試這樣做
return point.toString().substr(0, ele.toString().length) == ele;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/357453.html
標籤:javascript 数字 相比 传奇
上一篇:使用JavaScript/jQuery定位普通父母孩子
下一篇:加載JSON檔案并列印特定部件
