mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
initial commit
This commit is contained in:
parent
605a8e7d3a
commit
2e1bbd14e5
@ -41,6 +41,10 @@ function NumberFormatter (value) {
|
||||
this.exponent = exponent;
|
||||
}
|
||||
|
||||
NumberFormatter.prototype.toEngineering = function (precision) {
|
||||
return '0';
|
||||
};
|
||||
|
||||
/**
|
||||
* Format a number with fixed notation.
|
||||
* @param {number} [precision=0] Optional number of decimals after the
|
||||
|
||||
@ -60,6 +60,8 @@ exports.sign = function(x) {
|
||||
* For example '123.40' and '14000000'
|
||||
* 'exponential' Always use exponential notation.
|
||||
* For example '1.234e+2' and '1.4e+7'
|
||||
* 'engineering' Always use engineering notation.
|
||||
* For example '123.4e+0 and 14.0e+6'
|
||||
* 'auto' (default) Regular number notation for numbers
|
||||
* having an absolute value between
|
||||
* `lower` and `upper` bounds, and uses
|
||||
@ -99,6 +101,7 @@ exports.sign = function(x) {
|
||||
* format(12.071, {notation: 'fixed'}); // '12'
|
||||
* format(2.3, {notation: 'fixed', precision: 2}); // '2.30'
|
||||
* format(52.8, {notation: 'exponential'}); // '5.28e+1'
|
||||
* format(12345678, {notation: 'engineering'}); // '12.345678e+6'
|
||||
*
|
||||
* @param {number} value
|
||||
* @param {Object | Function | number} [options]
|
||||
@ -148,6 +151,9 @@ exports.format = function(value, options) {
|
||||
case 'exponential':
|
||||
return exports.toExponential(value, precision);
|
||||
|
||||
case 'engineering':
|
||||
return exports.toEngineering(value, precision);
|
||||
|
||||
case 'auto':
|
||||
return exports
|
||||
.toPrecision(value, precision, options && options.exponential)
|
||||
@ -177,6 +183,18 @@ exports.toExponential = function(value, precision) {
|
||||
return new NumberFormatter(value).toExponential(precision);
|
||||
};
|
||||
|
||||
/**
|
||||
* Format a number in engineering notation. Like '1.23e+6', '2.3e+0', '3.500e-3'
|
||||
* @param {number} value
|
||||
* @param {number} [precision] Number of digits in formatted output.
|
||||
* If not provided, the maximum available digits
|
||||
* is used.
|
||||
* @returns {string} str
|
||||
*/
|
||||
exports.toEngineering = function(value, precision) {
|
||||
return new NumberFormatter(value).toEngineering(precision);
|
||||
};
|
||||
|
||||
/**
|
||||
* Format a number with fixed notation.
|
||||
* @param {number} value
|
||||
|
||||
@ -60,6 +60,27 @@ describe('format', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('engineering notation', function () {
|
||||
it('should format positive single digit to engineering notation', function() {
|
||||
assert.equal(math.format(3, { notation: 'engineering' }), '3.0e0');
|
||||
});
|
||||
xit('should format positive two digits to engineering notation', function() {
|
||||
assert.equal(math.format(30, { notation: 'engineering' }), '30.0e0');
|
||||
});
|
||||
xit('should format positive three digits to engineering notation', function() {
|
||||
assert.equal(math.format(300, { notation: 'engineering' }), '300.0e0');
|
||||
});
|
||||
xit('should format positive four digits to engineering notation', function() {
|
||||
assert.equal(math.format(3000, { notation: 'engineering' }), '3.0e+3');
|
||||
});
|
||||
xit('should format positive uneven four digits to engineering notation', function() {
|
||||
assert.equal(math.format(3001, { notation: 'engineering' }), '3.001e+3');
|
||||
});
|
||||
xit('should format positive uneven ten digits to engineering notation', function() {
|
||||
assert.equal(math.format(3741293481, { notation: 'engineering' }), '3.741293481e+9');
|
||||
});
|
||||
});
|
||||
|
||||
describe('bignumber', function () {
|
||||
var bigmath = math.create({precision: 20}); // ensure the precision is 20 digits
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user