I have a string, 12345.00
, and I would like it to return 12345.0
.
I have looked at trim
, but it looks like it is only trimming whitespace and slice
which I don't see how this would work. Any suggestions?
I have a string, 12345.00
, and I would like it to return 12345.0
.
I have looked at trim
, but it looks like it is only trimming whitespace and slice
which I don't see how this would work. Any suggestions?
You can use the substring function:
This is the accepted answer, but as per the conversations below, the slice syntax is much clearer:
Here is an alternative that i don't think i've seen in the other answers, just for fun.
Not as legible or simple as slice or substring but does allow you to play with the string using some nice array methods, so worth knowing.
If you want to do generic rounding of floats, instead of just trimming the last character:
EDIT: Seems like there exists a built in function for this, as Paolo points out. That solution is obviously much cleaner than mine. Use parseFloat followed by toFixed
The easiest method is to use the
slice
method of the string, which allows negative positions (corresponding to offsets from the end of the string):If you needed something more general to remove everything after (and including) the last underscore, you could do the following (so long as
s
is guaranteed to contain at least one underscore):Use regex: