Skip to content

Instantly share code, notes, and snippets.

@nakov
Created February 8, 2023 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nakov/a6aad478c020df4ec2d97695d01dcfca to your computer and use it in GitHub Desktop.
Save nakov/a6aad478c020df4ec2d97695d01dcfca to your computer and use it in GitHub Desktop.
Shelly: Number to String without Trailing Zeros
function numberToStringWithoutTrailingZeros(num) {
let numStr = JSON.stringify(num);
let cleanNumStr = removeTrailingZeroes(numStr);
return cleanNumStr;
}
function removeTrailingZeroes(str) {
let index;
for (index = str.length-1; index >= 0; index--) {
if (str[index] !== '0')
break;
}
return str.slice(0, index+1);
}
let num = 123.02 + 0.111;
print("Num (standard):", num);
print("Num (no trailing zeros):",numberToStringWithoutTrailingZeros(num));
Num (standard): 123.131000
Num (no trailing zeros): 123.131
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment