How can I convert a string to a double precision decimal number without losing the leading zeros ?

Hi, I am trying to convert a string to a double precision number using str2double.
Since my string has leading zeros, i.e., '-0,01298784', when I convert it to double, it comes like -1298784. How can I make sure the leading zeros are still there after conversion?
Notice that is I don't know beforehand how many zeros are there, so it may vary.
Thank you.

 Accepted Answer

Is the comma in the string meant to be a decimal point? E.g.
s = '-0,01298784'
s(s==',') = '.'
str2double(s)

3 Comments

Complicate? It is a complicated idea to store numbers with a comma as decimal separator. For scientific computing the dot is apropriate. But:
s(s==',') = '.'
or
s = strrep(s, ',', '.')
is a smart and efficient way to adjust the strings on the fly. I think, that this is not complicated.
thank you. I always used regexprep to do this. I didnt know that booleans work on strings too!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!