@rayyamhk/complex
Version:
A lightweight and easy-to-use library for you to manipulate complex numbers
52 lines (40 loc) • 837 B
JavaScript
;
/**
* Gets the stringified and formatted Complex Number.
* @memberof Complex
* @instance
* @returns {string} The stringified and formatted Complex Number
*/
function toString() {
var re = this.re,
im = this.im;
if (Number.isNaN(re) || Number.isNaN(im)) {
return 'NaN';
}
if (re === 0 && im === 0) {
return '0';
}
if (re === 0) {
if (im === 1) {
return 'i';
}
if (im === -1) {
return '-i';
}
return "".concat(im, "i");
}
if (im === 0) {
return "".concat(re);
}
if (im > 0) {
if (im === 1) {
return "".concat(re, " + i");
}
return "".concat(re, " + ").concat(im, "i");
}
if (im === -1) {
return "".concat(re, " - i");
}
return "".concat(re, " - ").concat(Math.abs(im), "i");
}
module.exports = toString;