Skip to content

Percentage rounding error? (yet another issue about them) #1326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maniqui opened this issue May 15, 2013 · 4 comments · Fixed by #1814
Closed

Percentage rounding error? (yet another issue about them) #1326

maniqui opened this issue May 15, 2013 · 4 comments · Fixed by #1814

Comments

@maniqui
Copy link

maniqui commented May 15, 2013

I was tinkering with some Bootstrap mixins, in this case, the one for generating a grid system.
I noticed some weird rounding for one value (.col-sm-11-20) on the output when generating a simple 20-columns grid.
I tested both with 1.3.3 and 1.4.0-beta at http://less2css.org/

Is this a bug or anything to be worried about?

Input

//
// Mixins
// --------------------------------------------------

@grid-columns: 20;

// Small grid columns
.generate-grid-columns(@grid-columns) {

  .col-sm-X (@index) when (@index > 0) {
    .col-sm-@{index}-@{grid-columns} { .col-sm-(@index); }
    .col-sm-X((@index - 1));
  }
  .col-sm-X (0) {}

  // Generate columns
  .col-sm-X(@grid-columns);

  // Apply the styles
  .col-sm-(@columns) {
    width: percentage((@columns / @grid-columns));
  }
}

// Generate small grid classes first
.generate-grid-columns(@grid-columns);

Output

.col-sm-20-20 {
  width: 100%;
}
.col-sm-19-20 {
  width: 95%;
}
.col-sm-18-20 {
  width: 90%;
}
.col-sm-17-20 {
  width: 85%;
}
.col-sm-16-20 {
  width: 80%;
}
.col-sm-15-20 {
  width: 75%;
}
.col-sm-14-20 {
  width: 70%;
}
.col-sm-13-20 {
  width: 65%;
}
.col-sm-12-20 {
  width: 60%;
}
.col-sm-11-20 {
  width: 55.00000000000001%;
}
.col-sm-10-20 {
  width: 50%;
}
.col-sm-9-20 {
  width: 45%;
}
.col-sm-8-20 {
  width: 40%;
}
.col-sm-7-20 {
  width: 35%;
}
.col-sm-6-20 {
  width: 30%;
}
.col-sm-5-20 {
  width: 25%;
}
.col-sm-4-20 {
  width: 20%;
}
.col-sm-3-20 {
  width: 15%;
}
.col-sm-2-20 {
  width: 10%;
}
.col-sm-1-20 {
  width: 5%;
}
@lukeapage
Copy link
Member

thats strange.. but I assume its just JavaScripts terrible number system. I did think we rounded a certain number of places so we weren't effected by it - that then causes issues for people wanting a million decimal places.

Is this causing an issue or is it just that it looks strange? I wouldn't be too worried by it.

@maniqui
Copy link
Author

maniqui commented May 16, 2013

Haven't yet tested if it causes any issue, particularly when complementing it with one or some columns that add up to the remaining 45%. Will check whenever possible and let you know.

@matthew-dean
Copy link
Member

We could do some math to round numbers that are "very close" to their whole number values. That is, cap the number of decimal places.

@matthew-dean
Copy link
Member

Stack overflow says on a related issue:

"...due to the way floats are represented, you may not get a clean number that's to two decimal places. For display purposes, always do num.toFixed(2)."

I wonder if that's related. It also provides this link to explain floats, with a deep level of nerdiness: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants