Open main menu

CDOT Wiki β

Changes

User:Dhhodgin

1,472 bytes added, 00:10, 13 November 2009
code blocks
=== code blocks ===
Here is a list of code blocks I have written for the processing.js project
==== nfc() ====
Specification for nfc() in processing [http://processing.org/reference/nfc_.html here].<br />
nfc() is a function used to format numbers with commas every 3rd digit and optionally how many decimal places to show right of the decimal place. It accepts int, int[], float, and float[] types and returns either a single string or array of strings depending on the input.
 
p.nfc = function( num, right ){
var str;
var decimals = right >= 0 ? right : 0;
if (typeof num == "object"){
str = new Array();
for(var i=0; i < num.length; i++){
str[i] = p.nfc(num[i], decimals);
}
}
else if (arguments.length == 2){
var rawStr = p.nfs(num, 0, decimals);
var digits = ("" + Math.floor(Math.abs(rawStr))).length;
var ary = new Array();
ary = rawStr.split('.');
// ary[0] contains left of decimal, ary[1] contains decimal places if they exist
// insert commas now, then append ary[1] if it exists
var leftStr = ary[0];
var rightStr = ary.length > 1 ? '.' + ary[1] : '';
var commas = /(\d+)(\d{3})/;
while (commas.test(leftStr)){
leftStr = leftStr.replace(commas, '$1' + ',' + '$2');
}
str = leftStr + rightStr;
}
else if (arguments.length == 1){
str = p.nfc(num, 0);
}
return str;
}
 
Example of this function and test is [http://matrix.senecac.on.ca/~dhhodgin/dps909/nfc_test.htm here].<br />
'''Known issues:''' None.
==== shorten() ====
Specification for shorten() in processing [http://processing.org/reference/shorten_.html here].<br />
==== nfs() ====
Specification for nfs() in processing [http://processing.org/reference/nef_nfs_.html here].<br />
nfs() is a function used to format numbers as strings with padding of 0's on either the left or right of the decimal place. It accepts int, int[], float, and float[] types and returns either a single string or array of strings depending on the input.
1
edit