0

How to check if value is a number. If it is a number value display text center else text left. I want to know what is wrong with my statement.

 for (var l = 0; l < Object.keys(pods[i].data.summaryDetailedData).length; l++) {
    contentHtml += "<tr>";

    for (var m = 0; m < pods[i].data.columns.length; m++) {

        for (var field in pods[i].data.summaryDetailedData[l]) {
            var rowspan = (pods[i].data.summaryDetailedData[l].children !== undefined ? pods[i].data.summaryDetailedData[l].children.length : "");
            rowspanMax = Math.max(rowspanMax, rowspan);

            if (field === pods[i].data.columns[m]) {

                var preFix = pods[i].data.summaryDetailedData[l]["sPrefix"] !== undefined ? pods[i].data.summaryDetailedData[l]["sPrefix"] : "";
                var postFix = pods[i].data.summaryDetailedData[l]["sPostfix"] !== undefined ? pods[i].data.summaryDetailedData[l]["sPostfix"] : "";
                var value = pods[i].data.summaryDetailedData[l][field];
                if (value.toString().substr(0, 3) == "- 1") {
                    value = "N/A";
                }
                var color = pods[i].data.summaryDetailedData[l][field + "_color"];
                if (colName[m] === "sLabel" && pods[i].data.summaryDetailedData[l].bStepThrough == true) {
                    value = "<a href=\"#\" class=\"stepthrough\">" + value + "</a>";
                }
                color = color !== "" && color !== undefined ? " <span class=\"color\" style=\"background: #" + color + "\"></span>" : " <span class=\"color\"></span>";
                contentHtml += "<td rowspan1=\"" + 1 + "\" class=\"" + (rowspan !== "" && rowspan > 1 ? "groups" : "") + " " + (!isNaN(value) || (!isNaN(value.toString().substr(1, value.length)) || value == "N/A" || typeof value == Number) ? "text-center" : "text-left") + "\">" + value + (Number(value) ? preFix : "") + color + (!isNaN(value) ? postFix : "") + "</td>";
                if (rowspan > 1) {
                    var rowspanContent = "<td rowspa1=\"" + rowspan + "\" class=\"" + (rowspan !== "" && rowspan > 1 ? "groups" : "") + " " + (!isNaN(value) || (!isNaN(value.toString().substr(1, value.length)) || value == "N/A" || typeof value == Number) ? "text-center" : "text-left") + "\">" + value + (Number(value) ? preFix : "") + color + (!isNaN(value) ? postFix : "") + "</td>";
                }

                if (field === "sLabel") {
                    for (var child in pods[i].data.summaryDetailedData[l].children) {

                        if (child > 0 && rowspan >= 2) {
                            contentHtml += "</tr>";
                            contentHtml += "<tr>";
                            contentHtml += rowspanContent;
                        }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

1

You use typeof:

var ff=1.222; console.log(typeof ff) // number
(typeof ff == "number") // true
var ff="1.222"; console.log(typeof ff) // string
(typeof ff == "number") // false

OR

Using the toFixed method, if the variable is not a number, it returns undefined, otherwise, the number object will have the function defined in its prototype, so it will return the function toFixed itself hence it is a number.

(this is sort of a hack, but it is efficient and least cost)

contentHtml += "<td class=\"" 
  +  (value.toFixed) ? "text-center" : "text-left" + "\">" ) 
  + value + (value.toFixed) ? preFix : "" 
  + color + (value.toFixed) ? postFix : "" + "</td>";
KAD
  • 10,972
  • 4
  • 31
  • 73
0

By using Number.isFinite(value):

 contentHtml += "<td class=\"" +  (Number.isFinite(value) ? "text-center" : "text-left" + "\">" ) + value + (Number.isFinite(value) ? preFix : "") + color + (Number.isFinite(value) ? postFix : "") + "</td>";
manonthemat
  • 6,101
  • 1
  • 24
  • 49
0

This is the check currently used by jQuery 3.0:

function isNumeric(obj){
    // As of jQuery 3.0, isNumeric is limited to
    // strings and numbers (primitives or objects)
    // that can be coerced to finite numbers (gh-2662)
    var type = jQuery.type(obj);
    return (type === "number" || type === "string") &&
        // parseFloat NaNs numeric-cast false positives ("")
        // ...but misinterprets leading-number strings, particularly hex literals ("0x...")
        // subtraction forces infinities to NaN
        !isNaN(obj - parseFloat(obj));
}

So if you use jQuery anyway, why not use the built in isNumeric() functionality?

Taryn
  • 242,637
  • 56
  • 362
  • 405
Lain
  • 3,657
  • 1
  • 20
  • 27