App-SocialCalc-Multiplayer
view release on metacpan or search on metacpan
socialcalc/socialcalc-3.js view on Meta::CPAN
return displayvalue;
}
//
// displayvalue = format_number_for_display(rawvalue, valuetype, valueformat)
//
SocialCalc.format_number_for_display = function(rawvalue, valuetype, valueformat) {
var value, valuesubtype;
var scc = SocialCalc.Constants;
value = rawvalue-0;
valuesubtype = valuetype.substring(1);
if (valueformat=="Auto" || valueformat=="") { // cases with default format
if (valuesubtype=="%") { // will display a % character
valueformat = "#,##0.0%";
}
else if (valuesubtype=='$') {
valueformat = '[$]#,##0.00';
}
else if (valuesubtype=='dt') {
valueformat = scc.defaultFormatdt;
}
else if (valuesubtype=='d') {
valueformat = scc.defaultFormatd;
}
else if (valuesubtype=='t') {
valueformat = scc.defaultFormatt;
}
else if (valuesubtype=='l') {
valueformat = 'logical';
}
else {
valueformat = "General";
}
}
if (valueformat=="logical") { // do logical format
return value ? scc.defaultDisplayTRUE : scc.defaultDisplayFALSE;
}
if (valueformat=="hidden") { // do hidden format
return " ";
}
// Use format
return SocialCalc.FormatNumber.formatNumberWithFormat(rawvalue, valueformat, "");
}
//
// valueinfo = DetermineValueType(rawvalue)
//
// Takes a value and looks for special formatting like $, %, numbers, etc.
// Returns the value as a number or string and the type as {value: value, type: type}.
// Tries to follow the spec for spreadsheet function VALUE(v).
//
SocialCalc.DetermineValueType = function(rawvalue) {
var value = rawvalue + "";
var type = "t";
var tvalue, matches, year, hour, minute, second, denom, num, intgr, constr;
tvalue = value.replace(/^\s+/, ""); // remove leading and trailing blanks
tvalue = tvalue.replace(/\s+$/, "");
if (value.length==0) {
type = "";
}
else if (value.match(/^\s+$/)) { // just blanks
; // leave type "t"
}
else if (tvalue.match(/^[-+]?\d*(?:\.)?\d*(?:[eE][-+]?\d+)?$/)) { // general number, including E
value = tvalue - 0; // try converting to number
if (isNaN(value)) { // leave alone - catches things like plain "-"
value = rawvalue + "";
}
else {
type = "n";
}
}
else if (tvalue.match(/^[-+]?\d*(?:\.)?\d*\s*%$/)) { // percent form: 15.1%
value = (tvalue.slice(0, -1) - 0) / 100; // convert and scale
type = "n%";
}
else if (tvalue.match(/^[-+]?\$\s*\d*(?:\.)?\d*\s*$/) && tvalue.match(/\d/)) { // $ format: $1.49
value = tvalue.replace(/\$/, "") - 0;
type = "n$";
}
else if (tvalue.match(/^[-+]?(\d*,\d*)+(?:\.)?\d*$/)) { // number format ignoring commas: 1,234.49
value = tvalue.replace(/,/g, "") - 0;
type = "n";
}
else if (tvalue.match(/^[-+]?(\d*,\d*)+(?:\.)?\d*\s*%$/)) { // % with commas: 1,234.49%
value = (tvalue.replace(/[%,]/g, "") - 0) / 100;
type = "n%";
}
else if (tvalue.match(/^[-+]?\$\s*(\d*,\d*)+(?:\.)?\d*$/) && tvalue.match(/\d/)) { // $ and commas: $1,234.49
value = tvalue.replace(/[\$,]/g, "") - 0;
type = "n$";
}
else if (matches=value.match(/^(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{1,4})\s*$/)) { // MM/DD/YYYY, MM/DD/YYYY
year = matches[3] - 0;
year = year < 1000 ? year + 2000 : year;
value = SocialCalc.FormatNumber.convert_date_gregorian_to_julian(year, matches[1]-0, matches[2]-0)-2415019;
type = "nd";
}
else if (matches=value.match(/^(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})\s*$/)) { // YYYY-MM-DD, YYYY/MM/DD
year = matches[1]-0;
year = year < 1000 ? year + 2000 : year;
value = SocialCalc.FormatNumber.convert_date_gregorian_to_julian(year, matches[2]-0, matches[3]-0)-2415019;
type = "nd";
}
( run in 3.292 seconds using v1.01-cache-2.11-cpan-b16cb0d3907 )