Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ability to drop units on toString #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/quantities.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
*
* @returns {string} reparseable quantity as string
*/
toString: function(targetUnitsOrMaxDecimalsOrPrec, maxDecimals) {
toString: function(targetUnitsOrMaxDecimalsOrPrec, maxDecimals, onlyNumber) {
var targetUnits;
if(typeof targetUnitsOrMaxDecimalsOrPrec === "number") {
targetUnits = this.units();
Expand All @@ -783,14 +783,18 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
targetUnits = targetUnitsOrMaxDecimalsOrPrec;
}
else if(targetUnitsOrMaxDecimalsOrPrec instanceof Qty) {
return this.toPrec(targetUnitsOrMaxDecimalsOrPrec).toString(maxDecimals);
if (onlyNumber) {
return this.toPrec(targetUnitsOrMaxDecimalsOrPrec).toString(maxDecimals).split(' ')[0];
} else {
return this.toPrec(targetUnitsOrMaxDecimalsOrPrec).toString(maxDecimals);
}
}

var out = this.to(targetUnits);

var outScalar = maxDecimals !== undefined ? round(out.scalar, maxDecimals) : out.scalar;
out = (outScalar + " " + out.units()).trim();
return out;
return (onlyNumber) ? outScalar : out;
},

// Compare two Qty objects. Throws an exception if they are not of compatible types.
Expand Down