问题
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