fix rounding to 0 decimal places

This commit is contained in:
Stewart Allen 2022-11-08 22:43:19 -05:00
commit 8c83e1d482
3 changed files with 6 additions and 4 deletions

View file

@ -253,6 +253,7 @@ Math.bound = function(val, min, max) {
};
Number.prototype.round = function(digits) {
if (digits === 0) return this.valueOf() | 0;
const pow = Math.pow(10, digits || 3);
return Math.round(this.valueOf() * pow) / pow;
};

View file

@ -134,7 +134,9 @@ function inRange(value, min, max) {
}
function round(v, zeros) {
let pow = Math.pow(10, zeros || round_decimal_precision);
const prec = zeros !== undefined ? zeros : round_decimal_precision;
if (prec === 0) return v | 0;
let pow = Math.pow(10, prec);
return Math.round(v * pow) / pow;
}

View file

@ -131,7 +131,7 @@ CAM.export = function(print, online) {
function add0(val, opt) {
let s = val.toString(),
d = s.indexOf(".");
if (d < 0) {
if (d < 0 && decimals > 0) {
return opt ? s : s + '.0';
} else {
return val.toFixed(decimals);
@ -170,7 +170,6 @@ CAM.export = function(print, online) {
newpos.x = util.round(newpos.x, decimals);
newpos.y = util.round(newpos.y, decimals);
newpos.z = util.round(newpos.z, decimals);
// newpos.a = util.round(newpos.a || 0, decimals);
// on tool change
let changeTool = out.tool != pos.t;
@ -333,7 +332,7 @@ CAM.export = function(print, online) {
if (map.Y) scale.y = map.Y;
if (map.Z) scale.z = map.Z;
if (map.DEC !== undefined) decimals = parseInt(map.DEC);
console.log('scaling axes', scale);
console.log('export scaling applied', map);
} catch (e) {
console.log('malformed scale directive', line);
}