2

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)

RGBK
  • 2,048
  • 5
  • 35
  • 55

3 Answers3

6

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:

// 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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
2

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;
Shaoz
  • 10,573
  • 26
  • 72
  • 100
  • Because of functional scoping would the two values generated inside the function be invisible outside of the function? – Jason Sperske Mar 14 '12 at 16:04
  • @JasonSperske yes, they'd be invisible. – Alnitak Mar 14 '12 at 16:19
  • Yes they would as they're in the function scope. What can be done also is make the function return a object literal. check the answer – Shaoz Mar 14 '12 at 16:30
  • Calling it twice to get the two portions, seriously?! – Alnitak Mar 14 '12 at 17:24
  • @Alnitak: Don't 'seriously' but show me the correct way, please, as everyone here makes mistake and we learn from each other. And I'm still a newby in javascript anyway. So you should put people off. In any case, I also wanna know the correct way of writing javascript. Sorry about RGBK – Shaoz Mar 14 '12 at 17:41
  • You could set the return value to a var and then look at it's two values. I'm going to retire early with all the milliseconds I'll be saving :) – Jason Sperske Mar 14 '12 at 22:14
1

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.

kennebec
  • 102,654
  • 32
  • 106
  • 127