css calc - round down with two decimal cases
In general I would say that it's not possible, but there's a hack. However in the web, hacks seem to be the norm, instead of the exception, so I'll just say that it is possible:
div {
--shf: 4.9406564584124654e-322;
width: calc(((100% / 11) - 9.09px) * var(--shf) / var(--shf));
}
What this does: it multiplies the value to be rounded by a really small value that underflows the value starting at the third decimal point. Then, it divides the truncated value back, resulting in a rounded version of the value. This assumes that all browsers you support use 64-bit floating point values. If they don't, not only this will be wrong, it might return zero when using smaller floating point data types, completely breaking your page.
Change the exponent to -323 to round at the first decimal point and -324 to round at integer values.
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.