Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
num2str - leading zeros, is not working?

Subject: num2str - leading zeros, is not working?

From: martin Müllinger

Date: 22 Mar, 2013 09:16:15

Message: 1 of 6

Hi,

i have the following problem.

I need to convert an integer into a string and i want a leading zero.

3.1 --> 03.1
21.2222 --> 21.2
1 --> 01.0

I tried :
i=3.1
disp(['i=',num2str(i,'%02.1d')])
but the result is i=3.1e+000

I tried
i=3.1
disp(['i=',num2str(i,'%02.1f)])
but the result is i=3.1 without the leading zero?

Any idea whats worng here?

THX

Subject: num2str - leading zeros, is not working?

From: Nasser M. Abbasi

Date: 22 Mar, 2013 12:23:25

Message: 2 of 6

On 3/22/2013 3:16 AM, martin Müllinger wrote:
> Hi,
>
> i have the following problem.
>
> I need to convert an integer into a string and i want a leading zero.
>
> 3.1 --> 03.1
> 21.2222 --> 21.2
> 1 --> 01.0
>

Why not leading zero before 21.2 then?

Subject: num2str - leading zeros, is not working?

From: martin Müllinger

Date: 22 Mar, 2013 11:28:20

Message: 3 of 6

Because i need two digits before the comma.


"Nasser M. Abbasi" wrote in message <kihf04$9cl$1@speranza.aioe.org>...
> On 3/22/2013 3:16 AM, martin Müllinger wrote:
> > Hi,
> >
> > i have the following problem.
> >
> > I need to convert an integer into a string and i want a leading zero.
> >
> > 3.1 --> 03.1
> > 21.2222 --> 21.2
> > 1 --> 01.0
> >
>
> Why not leading zero before 21.2 then?

Subject: num2str - leading zeros, is not working?

From: Bruno Luong

Date: 22 Mar, 2013 12:17:07

Message: 4 of 6

"martin Müllinger" wrote in message <kih7gv$lc0$1@newscl01ah.mathworks.com>...
> Hi,
>
> i have the following problem.
>
> I need to convert an integer into a string and i want a leading zero.
>
> 3.1 --> 03.1
> 21.2222 --> 21.2
> 1 --> 01.0
>

>> sprintf('%04.1f\n', [3.1 21.222 1])

ans =

03.1
21.2
01.0

>> num2str( [3.1 21.222 1], '%04.1f\n')

ans =

03.1
21.2
01.0


% Bruno

Subject: num2str - leading zeros, is not working?

From: Saravanan Mani

Date: 22 Mar, 2013 12:59:16

Message: 5 of 6

"martin Müllinger" wrote in message <kih7gv$lc0$1@newscl01ah.mathworks.com>...
> Hi,
>
> i have the following problem.
>
> I need to convert an integer into a string and i want a leading zero.
>
> 3.1 --> 03.1
> 21.2222 --> 21.2
> 1 --> 01.0
>
> I tried :
> i=3.1
> disp(['i=',num2str(i,'%02.1d')])
> but the result is i=3.1e+000
>
> I tried
> i=3.1
> disp(['i=',num2str(i,'%02.1f)])
> but the result is i=3.1 without the leading zero?
>
> Any idea whats worng here?
>
> THX


Hello,

i=3.1;
disp(['i=',num2str(i,'0%2.1f')])
fprintf('0%2.2f\n',i)


Best Regards,
Saravanan Mani

Subject: num2str - leading zeros, is not working?

From: martin Müllinger

Date: 22 Mar, 2013 13:27:07

Message: 6 of 6

Hi,

thank you very much! I really appreciate your help.

Have a nice weekend
Martin

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us