1

I've set the width of a div to 330px by inspecting it with Chrome and then setting the width manually. When I read the width back with jQuery.css('width') it comes back as 329.77777767181396px.

Does anyone know why this is defaulting to floating point, or whatever it is, instead of an integer?

Thanks

3
  • I am not completely answering your question but you can use Math.round(), it works perfectly Commented Aug 29, 2014 at 1:31
  • Yes. I'm just wondering what got flipped, because it didn't used to do this. Commented Aug 29, 2014 at 1:32
  • you can refer to the W3c article on the answer i posted Commented Aug 29, 2014 at 1:40

1 Answer 1

3

you can refer to the w3c specification below if you are really curious about this

http://www.w3.org/TR/CSS21/syndata.html#length-units

the floating values are rounded when the page is rendered, the exact values are stored in the memory and used for its child element calculations. For example, if your div is 105.4999px it is rendered to 105px, it's child elements with a width of 50% will be calculated as .5*100.4999 instead of .5*100.

to complete the whole information about this they are called as subpixel and you can have further reading with John Resig's(jQuery creator) article about the problems with subpixels

http://ejohn.org/blog/sub-pixel-problems-in-css/

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for that. Where it gets me in trouble is when I try to save the value away in the user's DB record in a varchar 10 field, so that the next time they log in it's the default. It's just puzzling what changed to break this suddenly.
well its really safe to round off the values as the w3c specs said
Yes. It's not very clean here, though, because these .css() values come back as a string with 'px' on the end. So for each one (about 25) I have to strip off the 'px', get the raw number to an integer, do the Math.round, and then convert it back to a string with 'px' on the end. I'll do it. I was just hoping to find out what changed and change it back. I guess I could also increase the DB field length to accept all the digits.
I just looked at an older saved version of the code and the .css('width') returns and integer number: "330px".
This may be a clue: when I read boxShadow the values from .css(boxShadow) are rgb(68,68,68) 2px 2.9999...px, 5.9999...px 0px. It got x_off exact (2), but not y_off (2.99…) or blur_dist (5.99…), and then it got spread_dist (0) exact. It seems that the values themselves are sometimes "on grid" (2.00000..), in which case the floating point comes back as 2, and sometimes they're off a hair. What in the world could cause that???
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.