MATLAB Answers

how to set 1000 separator for large numbers in GUI MATLAB

53 views (last 30 days)
Natalia P.
Natalia P. on 24 May 2011
Commented: Juan Saenz-Diez on 18 May 2020
Hello! I am trying to find a way to set 1000 separator for the large numbers displayed in my GUI. For example: I would like the number 100000 to be displayed in my GUI like 100.000 or 100,000.
How could I do it? Thank you, in advance!

  1 Comment

Juan Saenz-Diez
Juan Saenz-Diez on 18 May 2020
Hello! I wonder if this is what Natalia was asking, and whether she got her answer. What I would like to do is what I think Natalia is asking: how to get Matlab to display "normal" numbers with 1000 separators automatically, not in our own code output. Like doing "format bank" but adding the ',' separator to the thousands (as any banking application would do, which is the goal). Anyway, that is what I am looking for. Thanks for helping!

Sign in to comment.

Answers (4)

Jan
Jan on 24 May 2011
Without Java:
S = sprintf('%.16g', pi * 1e12); % [EDITED: was '%16']
T(1:length(S)) = char(0);
T(strfind(S, '.') - 4:-3:1) = char(39); % [EDITED: -3 -> -4]
S = [S; T];
S = reshape(S(S ~= 0), 1, []);
>> 3'141'592'653'589.793

  2 Comments

Oleg Komarov
Oleg Komarov on 24 May 2011
I get:
>> S = sprintf('%16g', pi * 1e12)
S =
3.14159e+012
Also, 31'....'89.793 doesn't look correct.

Sign in to comment.



Oleg Komarov
Oleg Komarov on 24 May 2011
Another alternative w/o java:
n = pi * 1e12;
c = fix(log10(n)+1);
dec = 3;
fmt = [repmat('%c',1,mod(c,dec)) repmat('''%c%c%c',1,fix(c/dec)) '%s'];
sprintf(fmt, sprintf('%.3f',n))
ans =
3'141'592'653'589.793

  0 Comments

Sign in to comment.


Matt Fig
Matt Fig on 24 May 2011
Another alternative.
H = '123456.09'; % A string number.
S = regexp(H,'\.','split');
S{1} = fliplr(regexprep(fliplr(S{1}),'\d{3}(?=\d)', '$0,'));
H2 = [S{1},'.',S{2}]
Note that if H will never have a decimal amount, this becomes a simple one-liner. In this case,
H = '123456'; % A string number with no decimal point.
H2 = fliplr(regexprep(fliplr(H),'\d{3}(?=\d)', '$0,'))

  2 Comments

Oleg Komarov
Oleg Komarov on 24 May 2011
One liner:
fliplr(regexprep(fliplr(H),'(\d+\.)?\d{3}(?=\d)', '$0,'))
Matt Fig
Matt Fig on 25 May 2011
Nice Oleg, I looked for that but couldn't find it. Great work!
That lazy quantifier is a miracle worker, I need to spend more time with it.

Sign in to comment.

Tags