Skip to content

Commit

Permalink
Fix bug in Amount.ratio_human
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Clark committed Oct 1, 2015
1 parent 8edc3b1 commit 4676ade
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/core/amount.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions test/amount-test.js
Expand Up @@ -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');
});
Expand Down

0 comments on commit 4676ade

Please sign in to comment.