mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
82 lines
3.3 KiB
JavaScript
82 lines
3.3 KiB
JavaScript
// test format
|
|
var assert = require('assert');
|
|
var math = require('../../../index.js');
|
|
|
|
describe('format', function() {
|
|
|
|
it('should format numbers', function() {
|
|
assert.equal(math.format(2/7), '0.2857142857142857');
|
|
assert.equal(math.format(0.10400), '0.104');
|
|
assert.equal(math.format(1000), '1000');
|
|
assert.equal(math.format(2.4e-7), '2.4e-7');
|
|
assert.equal(math.format(2.4e-6), '2.4e-6');
|
|
assert.equal(math.format(2.4e-5), '2.4e-5');
|
|
assert.equal(math.format(2.4e-4), '2.4e-4');
|
|
assert.equal(math.format(2.3e-3), '0.0023');
|
|
assert.equal(math.format(2.3e-2), '0.023');
|
|
assert.equal(math.format(2.3e-1), '0.23');
|
|
assert.equal(math.format(2.3), '2.3');
|
|
assert.equal(math.format(2.3e1), '23');
|
|
assert.equal(math.format(2.3e2), '230');
|
|
assert.equal(math.format(2.3e3), '2300');
|
|
assert.equal(math.format(2.3e4), '23000');
|
|
assert.equal(math.format(2.3e5), '2.3e5');
|
|
assert.equal(math.format(2.3e6), '2.3e6');
|
|
});
|
|
|
|
describe('precision', function() {
|
|
it('should format numbers with given precision', function() {
|
|
assert.equal(math.format(1/3), '0.3333333333333333');
|
|
assert.equal(math.format(1/3, 3), '0.333');
|
|
assert.equal(math.format(1/3, 4), '0.3333');
|
|
assert.equal(math.format(1/3, 5), '0.33333');
|
|
assert.equal(math.format(math.complex(1/3, 2), 3), '0.333 + 2i');
|
|
});
|
|
|
|
it('should format complex numbers with given precision', function() {
|
|
assert.equal(math.format(math.complex(1/3, 1/3), 3), '0.333 + 0.333i');
|
|
assert.equal(math.format(math.complex(1/3, 1/3), 4), '0.3333 + 0.3333i');
|
|
});
|
|
|
|
it('should format matrices with given precision', function() {
|
|
assert.equal(math.format([1/3, 1/3], 3), '[0.333, 0.333]');
|
|
assert.equal(math.format([1/3, 1/3], 4), '[0.3333, 0.3333]');
|
|
assert.equal(math.format(math.matrix([1/3, 1/3]), 4), '[0.3333, 0.3333]');
|
|
});
|
|
|
|
it('should format units with given precision', function() {
|
|
assert.equal(math.format(math.unit(2/3, 'm'), 3), '0.667 m');
|
|
assert.equal(math.format(math.unit(2/3, 'm'), 4), '0.6667 m');
|
|
});
|
|
|
|
it('should format ranges with given precision', function() {
|
|
assert.equal(math.format(new math.type.Range(1/3, 4/3, 2/3), 3), '0.333:0.667:1.33');
|
|
});
|
|
|
|
});
|
|
|
|
it('should format numbers with correct number of digits', function() {
|
|
assert.equal(math.format(1000.000, 5), '1000');
|
|
assert.equal(math.format(1000.0010, 5), '1000'); // rounded off at 5 digits
|
|
assert.equal(math.format(math.pi, 5), '3.1416');
|
|
assert.equal(math.format(math.pi * 10000, 5), '31416');
|
|
assert.equal(math.format(math.pi / 100, 5), '0.031416');
|
|
assert.equal(math.format(math.e, 5), '2.7183');
|
|
});
|
|
|
|
it('should format strings', function() {
|
|
assert.equal(math.format('hello'), '"hello"');
|
|
});
|
|
|
|
it('should format arrays', function() {
|
|
assert.equal(math.format([[1,2],[3,4]]), '[[1, 2], [3, 4]]');
|
|
assert.equal(math.format([[math.unit(2/3, 'm'), 2/7],['hi', math.complex(2,1/3)]], 5),
|
|
'[[0.66667 m, 0.28571], ["hi", 2 + 0.33333i]]');
|
|
});
|
|
|
|
it('should format complex values', function() {
|
|
assert.equal(math.format(math.divide(math.complex(2,5),3)), '0.6666666666666666 + 1.6666666666666667i');
|
|
assert.equal(math.format(math.divide(math.complex(2,5),3), 5), '0.66667 + 1.6667i');
|
|
});
|
|
|
|
}); |