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)
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)
Share Improve this question asked Mar 14, 2012 at 15:44 RGBKRGBK 2,0485 gold badges35 silver badges55 bronze badges3 Answers
Reset to default 6You 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:
// return integer part - may be negative
Math.trunc = function(n) {
return (n < 0) ? Math.ceil(n) : Math.floor(n);
}
You can use this to get the fractional part. Note that if you supply a negative number then the result will also be negative.
// return fraction part
Math.frac = function(n) {
return n - Math.trunc(n);
}
The C99 library has a modf
function that given the two functions above could be emulated thus:
Math.modf = function(n) {
return [Math.trunc(n), Math.frac(n)];
}
i.e. it returns an array containing the integer part, and then the fractional part.
This should do:
var myValue = 3.6;
function twoNewVars(){
var newVar1 = Math.floor(3.6),
newVar2 = (myValue - Math.floor(myValue)).toFixed(1);
}
or make the function return an object literal, if you want to see the variables:
function twoNewVars(){
return {
newVar1: Math.floor(3.6),
newVar2: (myValue - Math.floor(myValue)).toFixed(1)
};
}
twoNewVars().newVar1;
twoNewVars().newVar2;
Because of floating points, most decimals do not return the number you expect.
3.6-3 returns 0.6000000000000001, for example.
You can multiply and round the number, or use toFixed and convert that to a number:
Number.prototype.decimalpart= function(sig){
sig= sig || 12;
return +((this%1).toFixed(sig));
}
3.6.decimalpart()>>0.6
Splitting a string may be simpler.