Fix bug in Amount.ratio_human

This commit is contained in:
Chris Clark
2015-10-01 11:45:12 -07:00
parent 8edc3b1f36
commit 4676ade4ee
2 changed files with 12 additions and 5 deletions

View File

@@ -120,18 +120,14 @@ Amount.from_components_unsafe = function(value: Value, currency: Currency,
// be sure that _is_native is set properly BEFORE calling _set_value
Amount.prototype._set_value = function(value: Value) {
this._value = value.isZero() && value.isNegative() ?
value.negate() : value;
this._check_limits();
};
// Returns a new value which is the absolute value of this.
Amount.prototype.abs = function() {
return this._copy(this._value.abs());
};
Amount.prototype.add = function(addend) {
@@ -229,7 +225,7 @@ Amount.prototype.ratio_human = function(denom, opts) {
//
// To compensate, we multiply the numerator by 10^xns_precision.
if (denominator._is_native) {
numerator._set_value(numerator.multiply(bi_xns_unit));
numerator._set_value(numerator._value.multiply(bi_xns_unit));
}
return numerator.divide(denominator);

View File

@@ -1048,6 +1048,17 @@ describe('Amount', function() {
});
describe('ratio_human', function() {
it('Divide USD by XRP', function() {
const a = Amount.from_json({
value: '0.08161672093323858',
currency: 'USD',
issuer: 'rLFPPebckMYZf3urdomLsaqRGmQ6zHVrrK'
});
const b = Amount.from_json('15000000');
const c = a.ratio_human(b);
assert.deepEqual(c.to_json(), {value: '0.005441114728882572',
currency: 'USD', issuer: 'rLFPPebckMYZf3urdomLsaqRGmQ6zHVrrK'});
});
it('Divide USD by XAU (dem)', function() {
assert.strictEqual(Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').ratio_human(Amount.from_json('10/015841551A748AD2C1F76FF6ECB0CCCD00000000/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh'), {reference_date: 443845330 + 31535000}).to_text_full(), '201.0049931765529/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});