css calc - round down with two decimal cases

时光总嘲笑我的痴心妄想 提交于 2019-12-23 07:21:58

问题


I have the following situation:

div {
    width: calc((100% / 11) - 9.09px);
}

In the context, 100% = 1440px, and 9.09px is generated in mathematics with sass.

The results is: 94.55px, because the calc function rounds up, but I need 94.54px (round down).

How can I round down to the nearest hundredths place?

#Edit: example.


回答1:


Unfortunately, there is not a native way in CSS to round (or ceil/floor) numbers.

However — you mentioned you are using Sass. I found a small Sass library that can round, floor, and ceil numbers to a specified precision.

For example, if you had a had 94.546 you could use decimal-floor(94.546, 2) which would return 94.54.

Unfortunately, this might not help if you have to use calc() to calculate on the fly with CSS. However, if you can pre-calculate the width and floor it with Sass it would fit your needs. A possible solution could be using @media queries as a way to set breakpoints and use those breakpoints in your Sass preprocessing.



来源:https://stackoverflow.com/questions/37754542/css-calc-round-down-with-two-decimal-cases

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!