Add Base.formatNumber() to format numbers in the same way as Scriptographer (precision of up to 5 fractional digits) and use it in the various #toString() functions.

This commit is contained in:
Jürg Lehni 2011-05-04 19:42:50 +01:00
parent fb6955e509
commit bc80c58558
8 changed files with 31 additions and 20 deletions

View file

@ -193,9 +193,11 @@ var Matrix = this.Matrix = Base.extend({
* @return {string} A string representation of this transform.
*/
toString: function() {
return '[['
+ [this._m00, this._m01, this._m02].join(', ') + '], ['
+ [this._m10, this._m11, this._m12].join(', ') + ']]';
var format = Base.formatNumber;
return '[[' + [format(this._m00), format(this._m01),
format(this._m02)].join(', ') + '], ['
+ [format(this._m10), format(this._m11),
format(this._m12)].join(', ') + ']]';
},
/**

View file

@ -454,7 +454,8 @@ var Point = this.Point = Base.extend({
},
toString: function() {
return '{ x: ' + this.x + ', y: ' + this.y + ' }';
var format = Base.formatNumber;
return '{ x: ' + format(this.x) + ', y: ' + format(this.y) + ' }';
},
statics: {

View file

@ -221,10 +221,11 @@ var Rectangle = this.Rectangle = Base.extend({
},
toString: function() {
return '{ x: ' + this.x
+ ', y: ' + this.y
+ ', width: ' + this.width
+ ', height: ' + this.height
var format = Base.formatNumber;
return '{ x: ' + format(this.x)
+ ', y: ' + format(this.y)
+ ', width: ' + format(this.width)
+ ', height: ' + format(this.height)
+ ' }';
},

View file

@ -86,7 +86,9 @@ var Size = this.Size = Base.extend({
},
toString: function() {
return '{ x: ' + this.width + ', y: ' + this.height + ' }';
var format = Base.formatNumber;
return '{ x: ' + format(this.width)
+ ', y: ' + format(this.height) + ' }';
},
statics: {

View file

@ -173,13 +173,14 @@ var Color = this.Color = Base.extend(new function() {
},
toString: function() {
var parts = [];
var parts = [],
format = Base.formatNumber;
for (var i = 0, l = this._components.length; i < l; i++) {
var component = this._components[i];
var value = this['_' + component];
var component = this._components[i],
value = this['_' + component];
if (component === 'alpha' && value == null)
value = 1;
parts.push(component + ': ' + value);
parts.push(component + ': ' + format(value));
}
return '{ ' + parts.join(', ') + ' }';
},

View file

@ -83,6 +83,10 @@ Base.inject({
return str.replace(/\b[a-z]/g, function(match) {
return match.toUpperCase();
});
},
formatNumber: function(num) {
return (Math.round(num * 100000) / 100000).toString();
}
});

View file

@ -129,8 +129,8 @@ CurveLocation = Base.extend({
},
toString: function() {
var parts = [];
var point = this.getPoint();
var parts = [],
point = this.getPoint();
if (point)
parts.push('point: ' + point);
var index = this.getIndex();
@ -138,7 +138,7 @@ CurveLocation = Base.extend({
parts.push('index: ' + index);
var parameter = this.getParameter();
if (parameter != null)
parts.push('parameter: ' + parameter);
parts.push('parameter: ' + Base.formatNumber(parameter));
return '{ ' + parts.join(', ') + ' }';
}
});

View file

@ -42,10 +42,10 @@ var ToolEvent = this.ToolEvent = Base.extend({
toString: function() {
return '{ type: ' + this.type
+ ', point: ' + this.point
+ ', count: ' + this.count
+ ', modifiers: ' + this.modifiers
+ ' }';
+ ', point: ' + this.point
+ ', count: ' + this.count
+ ', modifiers: ' + this.modifiers
+ ' }';
},
/**