Javascript 在javascript中删除字符串或数字的最后3个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31489413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove last 3 characters of string or number in javascript
提问by teddybear123
I'm trying to remove last 3 zeroes 1437203995000
我正在尝试删除最后 3 个零 1437203995000
How do i this in javascript. Im generating the numbers from new date() function
我如何在 javascript 中执行此操作。我从新的 date() 函数生成数字
回答by Hari Das
Here is an approach using str.slice(0, -n)
.
Where nis the number of characters you want to truncate.
这是一种使用str.slice(0, -n)
. 其中n是要截断的字符数。
var str = 1437203995000;
str = str.toString();
console.log("Original data: ",str);
str = str.slice(0, -3);
str = parseInt(str);
console.log("After truncate: ",str);
回答by TaoPR
Remove last 3 characters of a string
删除字符串的最后 3 个字符
var str = '1437203995000';
str = str.substring(0, str.length-3);
// '1437203995'
Remove last 3 digits of a number
删除数字的最后 3 位
var a = 1437203995000;
a = (a-(a%1000))/1000;
// a = 1437203995