在SASS中除以1/3
的結果爲33.33333
,並且瀏覽器將此大小的三個元素總和爲99.984
。我需要的正是數字100
,這可以通過堆疊尺寸爲33.33334
的三個元素來實現。我怎樣才能迫使SASS整理最後一個分區號?SASS如何將浮點結果的1/3分割爲33.33334?
演示:http://sassmeister.com/gist/577dec6f9b85b70c3621
在SASS中除以1/3
的結果爲33.33333
,並且瀏覽器將此大小的三個元素總和爲99.984
。我需要的正是數字100
,這可以通過堆疊尺寸爲33.33334
的三個元素來實現。我怎樣才能迫使SASS整理最後一個分區號?SASS如何將浮點結果的1/3分割爲33.33334?
演示:http://sassmeister.com/gist/577dec6f9b85b70c3621
好了,解決辦法是使用由特克爾寫外部函數。
他的小數四捨五入功能:https://gist.github.com/terkel/4373420
使用
.tile {
&-1-3 {
width: decimal-ceil(100/3, 5);
// this should result as 33.33334%;
}
&-2-3 {
width: 2/3 * 100%;
}
&-1-1 {
width: 3/3 * 100%;
}
}
你必須使用滾乘法,除法和ceil()
自己的舍入方法。
.tile {
&-1-3 {
$num: 1/3; // 0.33333
// the number we multiply by should have the same number of
// zeros as there are decimal places you want to keep + the
// number of digits there will be on the left side of the decimal
$num: $num * 10000000; // 3333333.33333
// ceil to round up
$num: ceil($num); // 3333334
// divide by that amount again
$num: $num/10000000; // 33.33334
width: $num * 100%; // 33333.34%
}
}
爲什麼我對這個問題投下了票? –