YUI 3 Number Gallery Component

A while back I threw together a quick API port of the number.js from the YUI-Ext-MVC framework into the YUI 3 gallery. However, I did not give the API enough attention in my first pass, nor have I revisited it since. Today, I finally revisited the API to clean it up and add some sorely needed functions.

Getting started

You will need to include the module in your code:
	filter: raw,
	combine: true,
	timeout: 10000,
	modules: {
		gallery-number: {
			fullpath: http://github.com/mattsnider/yui3-gallery/blob/master/src/gallery-number/js/number.js,
			requires: [],
			optional: [],
			supersedes: []
}).use(gallery-number, function(Y) {
	/* … */

How to do it…

Convert radians to degrees and vice-versa:

var degreeValue = Y.Number.degrees(radianValue);
var radianValue = Y.Number.radians(degreeValue);

Evaluate if a number is between or not two other numbers:

var bool = Y.Number.isBetween(number, lowerBound, upperBound);
var bool = Y.Number.isNotBetween(number, lowerBound, upperBound);

Evaluate if a number is even or odd:

var bool = Y.Number.isEven(number);
var bool = Y.Number.isOdd(number);

Evaluate is a number is prime:

var bool = Y.Number.isPrime(number);

Find the precision of a number (number of decimal places):

var precision = Y.Number.getPrecision(number);

Generate a random integer:

var number = Y.Number.random(number);

Round to the nearest digit:

var number = Y.Number.roundToDigit(number, 100);

Format a number into a US number string:

var string = Y.Number.format(number, "0,000.00");

How it works…

The radians/degrees conversion functions uses the math equation, where "degree = radian * 180 / PI" to convert the values.

The isBetween/isNotBetween evaluation functions use the less than and greater than operators to evaluate where the first argument lies.

The isEven/isOdd evaluation functions use the modulus operator to see if the result equals ZERO or not.

The isPrime uses a series of rules to determine if the number is prime or not. First, it checks if the number is 2 or 5, as these are special primes. Then it checks if the number is not event. If the number is less than 200 it evaluates if the number is any of the primes less than 200 (these values are hardcoded). If the number is greater than 200, it checks if it is divisible by any of the prime less than 200.

The getPrecision function converts the number to an a string and splits around the decimal point. The length of the second parameter in the resulting array is the precision.

The random function uses a well-known equation to convert the decimal produced by Math.random into an integer value between ZERO and the number provided as the first argument.

The roundToDigit attempts to convert the provided value to the nearest digit, as determined by the second argument. This means if you have the number 799 and desire hundreds place rounding (pass in 100 as the second parameter) and the number will be rounded to 800, but if you round to the thousands place, the number will be rounded to 1000. This function works by converting the number to a string, moving the decimal place to desired precision, executing Math.round() and then converting the number back to the correct precision.

The format function uses a regular expression to find the textual number inside of the format string. It then applies logic to convert the number into the desired format; generally this means do you want commas and/or decimal places shown. And finally it inserts the formatted number back into the string. Therefore, you can pass in a complete sentence or a string of symbols, and this method surgically replace the number format string with the formatted number.

There&rsquot;s more…

The isBetween/isNotBetween evaluation functions can accept an optional boolean value as there fourth parameter. When true, they will perform inclusive comparisons, evaluating with the greater/less than or equal to operators, instead of greater/less than operators.

The random function accepts an optional second argument, which is the starting point of the random range. This way you can find random numbers between two numbers, instead of just between ZERO and the desired number.

Previously, this component wrapped some of the native Math functions, but this only added bloat, making the component larger than it needed to be, so I removed them.

See also

More to come

The functions included here are all derived from the math functions that I have needed for work on Mint.com, a financial site. However, there are probably other functions that are commonly used by developers, which I would like to include in this API. If you have a math function that you commonly use, please include it in the comments section.

Additionally, I think the format function should work more like a print function in C-style languages, so I will be improving this function eventually.