Created
February 8, 2023 13:35
-
-
Save nakov/a6aad478c020df4ec2d97695d01dcfca to your computer and use it in GitHub Desktop.
Shelly: Number to String without Trailing Zeros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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