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.

 Respuesta aceptada

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

3 comentarios

Yes. I think that is what complicate things.
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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by