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

10 views (last 30 days)
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

James Tursa
James Tursa on 18 Jul 2012
Is the comma in the string meant to be a decimal point? E.g.
s = '-0,01298784'
s(s==',') = '.'
str2double(s)
  3 Comments
Jan
Jan on 18 Jul 2012
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!