Skip to content
Advertisement

Support negative values

I’m in a situation where the CSV file is getting rid of the leading zero before my import and I need to be able to account for that. Let’s say that I have my value as the following:

-.0982739 -> I would want all case scenarios where it’s -. to turn into -0. – Here are my attempts:

JavaScript

To simplify things, basically any . that has nothing before it, add in a 0, otherwise use the value given. I will need it for both longitude and latitude.

Advertisement

Answer

JavaScript

The above regex matches the signs - and + if they are present and immediately followed by a .. Now, capture the matched group 1 in the regex which is ([-+])? and append 0. followed by all digits after . by taking substr of the current string.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement