最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is typeof my variable an object, and not a number - Stack Overflow

programmeradmin6浏览0评论

I have a table which has a couple cells that contain simple numbers (IE: 1.00, 1000.00, 10000.00). I'm trying to format the cell contents using the 'format' function below. I have had success with this function in a different area of my code, but for whatever reason (the reason why I'm here) when I try to feed the contents of the table cells in, it doesn't work as I expected.

The problem is that the typeof my cell contents is 'object' and not 'number', so it skates right by the if statement and just returns my original value back to me. Is there a way I can coerce the data to be typeof number? I thought var n = new Number(cellText); would do the trick, however, the typeof es back as object. Confused.

In globalize.js:

Globalize.format = function( value, format, cultureSelector ) {
    culture = this.findClosestCulture( cultureSelector );
    if ( value instanceof Date ) {
        value = formatDate( value, format, culture );
    }
    else if ( typeof value === "number" ) {
        value = formatNumber( value, format, culture );
    }
    return value;
};

In my page:

$(document).ready(function () {
    $('td[globalize="true"]').each(function () {
        var $this = $(this);
        var cellText = $this.text();
        if (cellText != null) {
            var n = new Number(cellText);
            var v = Globalize.formatNumber(n, _gloNum[0]);
            $this.text(v);
        }
    })
});

I have a table which has a couple cells that contain simple numbers (IE: 1.00, 1000.00, 10000.00). I'm trying to format the cell contents using the 'format' function below. I have had success with this function in a different area of my code, but for whatever reason (the reason why I'm here) when I try to feed the contents of the table cells in, it doesn't work as I expected.

The problem is that the typeof my cell contents is 'object' and not 'number', so it skates right by the if statement and just returns my original value back to me. Is there a way I can coerce the data to be typeof number? I thought var n = new Number(cellText); would do the trick, however, the typeof es back as object. Confused.

In globalize.js:

Globalize.format = function( value, format, cultureSelector ) {
    culture = this.findClosestCulture( cultureSelector );
    if ( value instanceof Date ) {
        value = formatDate( value, format, culture );
    }
    else if ( typeof value === "number" ) {
        value = formatNumber( value, format, culture );
    }
    return value;
};

In my page:

$(document).ready(function () {
    $('td[globalize="true"]').each(function () {
        var $this = $(this);
        var cellText = $this.text();
        if (cellText != null) {
            var n = new Number(cellText);
            var v = Globalize.formatNumber(n, _gloNum[0]);
            $this.text(v);
        }
    })
});
Share Improve this question asked Feb 22, 2012 at 17:29 HashTagDevDudeHashTagDevDude 5841 gold badge4 silver badges19 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

The problem is that the typeof my cell contents is 'object' and not 'number'

When you do:

new Number

You are creating new instance of Number object, that's why it gives you object and not number.

Is there a way I can coerce the data to be typeof number?

var n = +(cellText);

Or

var n = Number(cellText);

In JavaScript new Number returns a Number object. Have a look at parseFloat or parseInt.

Change:

var n = new Number(cellText);

To

var n = Number(cellText);

Or

var n = parseFloat(cellText);

Or

var n = parseInt(cellText, 10);

Depending on what you need.

new Number(cellText) returns a Number object, not a number primitive.

Use parseInt or parseFloat instead.

var cellText = '12.34',
a = new Number(cellText), // 12.34, but a Number object
b = parseInt(cellText, 10), // 12
c = parseFloat(cellText); // 12.34

typeof a; // 'object'
a instanceof Number; // true

typeof b; // 'number'
typeof c; // 'number'

The typeof is buggy in JavaScript. I suggest you use the following function instead:

function typeOf(value) {
    if (value === null) return "null";
    else if (typeof value === "undefined") return "undefined";
    else return Object.prototype.toString.call(value).slice(8, -1);
}
发布评论

评论列表(0)

  1. 暂无评论