kando
kando

Reputation: 605

Matlab: Convert number to string (with zero-padding AND decimal precision)

In Matlab, the num2str command converts numbers to strings.

A format input is allowed which permits the user the number format,
including options for zero-padding and precision.

Is there a way to create a string which zero-pads the number to the left
AND permits specified decimal precision?

I'm not getting results which demonstrate this:

Examples of Problem

Input:
num2str(1.2,'%04f')
Output:
1.200000

Input:
num2str(1.2,'%04.f')
Output:
0001

Input:
num2str(1.2,'%04.2f')
Output:
1.20

Note that zero-padding only occurs in a very specific format.

Desired behavior

Input:
num2str(1.2,'%04.2f')
Output:
0001.20

Upvotes: 1

Views: 2274

Answers (1)

EBH
EBH

Reputation: 10450

Write:

num2str(1.2,'%07.2f')

the number befor the . is the total number of digits + the ..

So 7.2 means: 7 total characters, from them 2 are after the ..

Upvotes: 2

Related Questions