I'm trying to inherit from Number to override the toString method to output fixed decimal points when toString is called:
function FixedNumber(value) {
Number.call(this, value);
}
util.inherits(FixedNumber, Number);
FixedNumber.prototype.toString = function() {
return this.toFixed(3);
}
var n = new FixedNumber(5);
var s = n.toString();
Unfortunately, this doesn't work. I get the following exception:
TypeError: Number.prototype.valueOf is not generic
at FixedNumber.valueOf (native)
at FixedNumber.toFixed (native)
at FixedNumber.toString (repl:2:13)
at repl:1:3
at REPLServer.self.eval (repl.js:110:21)
at repl.js:249:20
at REPLServer.self.eval (repl.js:122:7)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
What have I done wrong, and how do I do what I want?