I want to create a new variable based on whatever decimal values a variable has.
i.e:
myValue = 3.6
function twoNewVars{...}
newVar1 = 3
newVar2 = 0.6
Using jQuery or javascript.
Would one split that by simply looking for the position of the “.”?
Perhaps treat the digit “3.6” as a literal string and then split it… but that seems a bit messy, hopefully there’s a more elegant way to do that.
(NB Not at all required to understand the question – I need this in order to position elements on a page in a cleaner way. I have viewport height which can be anything, and then content, which is based on a fixed height, if i can find the discrepancy between the fixed height of the repeated content and the arbitrary height of the viewport i can automatically position things to cut cleanly)
You can use the difference between the original number and the nearest value to zero.
However Javascript provides no method to get the “nearest value to zero”. This adds that method:
You can use this to get the fractional part. Note that if you supply a negative number then the result will also be negative.
The C99 library has a
modffunction that given the two functions above could be emulated thus:i.e. it returns an array containing the integer part, and then the fractional part.