我有一个字符串12345.00,我希望它返回12345.0。

我看了一下trim,但它看起来只是在修剪空白和切片,我不知道这是如何工作的。有什么建议吗?


当前回答

试试看:

const myString=“Hello World!”;console.log(myString.slice(0,-1));

其他回答

试试看:

const myString=“Hello World!”;console.log(myString.slice(0,-1));

对于像您这样的数字,我建议对子字符串执行以下操作:

console.log(parseFloat('12345.00').toFixed(1));

请注意,这实际上会将数字四舍五入,我认为这是需要的,但可能不是:

console.log(parseFloat('12345.46').toFixed(1));

if(str.substring(str.length - 4) == "_bar")
{
    str = str.substring(0, str.length - 4);
}

正则表达式是您要查找的:

让str=“foo_bar”;console.log(str.replace(/_bar$/,“”));

https://stackoverflow.com/questions/34817546/javascript-how-to-delete-last-two-characters-in-a-string

如果不需要空间,只需使用修剪

"11.01 °C".slice(0,-2).trim()