Adam Scot
Adam Scot

Reputation: 1409

How many digits should I round recurring numbers to in css?

Let's say I want to set an element to have be 2/3 width of the page.

We could use something like:

width: 66.66666666666666666666666666666666666666666666%

I imagine at a certain point that the browser will stop taking note of the digits, but at what point is this?

Are there any more things we should be aware of when using recurring decimals in css?

Upvotes: 2

Views: 354

Answers (2)

Bob Goddard
Bob Goddard

Reputation: 977

A 5k screen is 5120 pixels wide. Taking 0.6, then 0.66 etc gives:

5120 * 0.6 = 3072.0
5120 * 0.66 = 3379.20
5120 * 0.666 = 3409.920
5120 * 0.6666 = 3412.9920
5120 * 0.66666 = 3413.29920
5120 * 0.666666 = 3413.329920

I'll leave it to you to work out if it's worth a few pixels either way between 0.666 and above, or even if 34 pixels is acceptable between 0.66 & 0.666.

Upvotes: 2

tr1
tr1

Reputation: 114


you can just use this:
width: calc(100% / 3); or width: calc(200% / 3) (in your case)

I usually round the value to one decimal place. I think, that's enough.

Upvotes: 5

Related Questions