Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Is a number whole? and Is a number a multiple of x?

  1. #1
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default Is a number whole? and Is a number a multiple of x?

    Here are a couple functions I keep using to validate numbers.
    Is a number whole?
    Code:
    function isWhole(x)
    {
    	var number = x;
    	var rounded = Math.round(number);
    	if (number == rounded)
    	{
    		//What do do if the number is whole
    	}
    	else
    	{
    		//What to do if the number is not whole
    		//or the number is not a number
    	}
    }
    Just replace the commented lines with useful code.
    I find it extremely useful to use something like return "The number was whole.";
    This way, you can use <? echo 'var input = '.$_GET['input'].';'; ?>
    var result = isMultiple(input);

    To call this function, use:
    Code:
    isMultiple(number)
    Is a number a multiple of x?
    This is a little trickier.
    Code:
    function isMultiple(y,z)
    {
    	var dividee = y;
    	var divider = z;
    	var divided = dividee / divider;
    	var dividem = Math.round(divided);
    	if (divided == dividem)
    	{
    		//What do do if the number is a multiple
    	}
    	else
    	{
    		//What to do if the number is not a multiple
    		//or the number is not a number
    	}
    }
    Do the same with the commented code.
    To call this one, use:
    Code:
    isMultiple(number,is_a_multiple_of_?)
    (is_a_multiple_of_? should be the number to check if number is a multiple of.)
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    For the whole thing, I'm assuming this is easier:

    Code:
    Number.prototype.whole = function() { return this.toString().indexOf(".") > -1 ? false : true; }
    Usage:

    Code:
    var num = 4.5294;
    if (num.whole()) {
      //do something
    } else {
      //do something else
    }
    - Mike

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    In addition:

    Code:
    Number.prototype.multipleOf = function(x) { return (x / this).whole() ? true : false; }
    - Mike

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Update:
    Code:
    Number.prototype.multipleOf = function(x) { return (x/this).whole() ? (arguments[1] ? (x/this) : true) : false; }
    Optional flag, which returns the remainder.
    - Mike

  5. #5
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    Who said it wasn't?
    Mine would be easier for beginner coders to understand.
    Which is pretty much the point.
    Go figure.
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Once again updated, I forgot to include the arguments[1] for the false:

    Code:
    Number.prototype.multipleOf = function(x) { return arguments[1] ? this/x : ((this/x).whole() ? true : false); }
    - Mike

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Well the readers don't need to know what's going on, I just think:

    Code:
    (x).whole();
    and

    Code:
    (x).multipleOf(y);
    are easy to use.
    - Mike

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Oops... I had my variables mixed up too:

    Code:
    Number.prototype.multipleOf = function(x) { return arguments[1] ? x/this : ((x/this).whole() ? true : false); }
    - Mike

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    Number.prototype.isMultipleOf(x) {
      return !(this % x);
    };
    
    Number.prototype.isWhole() {
      return this.isMultipleOf(1);
    };
    ... of course actually bothering to write functions for these starts to seem a little silly...
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Lol, I KNEW you would come along and do that (another way of writing the function). But I see what you're saying, they are really simple.
    - Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •