覆寫 Number.toString() 函式 @ Ticore's Blog :: 隨意窩 Xuite日誌
  • Site Search
  • Google
    Web Ticore's Blog
  • 最新文章
  • Recent Comments
  • 2005-09-27 20:49 覆寫 Number.toString() 函式
    平均分數:0 顆星    投票人數:0
    我要評分:
    上次Po過 Flash ActionScript 的 Number.toString() 有 Bug (Flash 數字進位轉換Bug)

    所以寫了一個函式覆寫原始的toString

    要考慮到能夠適用於 2 ~ 32 所有的進位

    不只是用於16、32進位~~

    最後想到以下的方式來做

    居然只有四行就可以完成,比一開始的寫法少很多,真是出乎意料




    ActionScript Number.toString() Overriding
    Number.prototype.__toString = Number.prototype.toString;
    Number.prototype.toString = function(radix) {
    return (Number(this) > radix ? (this / radix)["__toString"](radix) : "") +
    (this % radix)["__toString"](radix);
    };
    //
    trace(0xFFFF0090.toString(5));
    trace(0x90000090.toString(5));
    trace(0x00090.toString(5));
    trace(0xFFFF.toString(5));
    trace(0x0.toString(5));
    //
    trace(0xFFFF0090.toString(8));
    trace(0x90000090.toString(8));
    trace(0x00090.toString(8));
    trace(0xFFFF.toString(8));
    trace(0x0.toString(8));
    //
    trace(0xFFFF0090.toString(16));
    trace(0x90000090.toString(16));
    trace(0x00090.toString(16));
    trace(0xFFFF.toString(16));
    trace(0x0.toString(16));
    //
    Ticore / Xuite日誌 / 回應(1) / 引用(0) / 好文轉寄
    回應