Azzera filtri
Azzera filtri

How do I insert the comma marker for thousands into large numbers while printing to file or Command Line in MATLAB 7.7 (R2008b)?

27 visualizzazioni (ultimi 30 giorni)
I would like my numbers to appear with comma markers for thousands. For instance, I would like to display the number 1200387 as 1,200,387

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 28 Mag 2021
Modificato: MathWorks Support Team il 28 Mag 2021
To add comma separators between groups of three numbers to indicate the thousands place of a large number, save the following code to a file called "ThousandSep.m":
function out = ThousandSep(in)
%THOUSANDSEP adds thousands Separators to a 1x1 array.
% Example:
% ThousandSep(1234567)
import java.text.*
v = DecimalFormat;
out = char(v.format(in));
Then you can use the ThousandSep function (as defined above), to obtain a string with comma separated digits using the following line of code:
ThousandSep(123456778)
The resulting answer is:
ans =
123,456,778

Più risposte (1)

Toshiaki Takeuchi
Toshiaki Takeuchi il 14 Nov 2023
Using pattern
vec = 123456789;
txt = string(vec);
pat1 = lookBehindBoundary(digitsPattern); % (?<=\d)
pat2 = asManyOfPattern(digitsPattern(3),1); % (\d{3})+
pat3 = lookAheadBoundary(pat2+lineBoundary("end")); % (?=(\d{3})+$)
pat4 = pat1+pat3; % (?<=\d)(?=(\d{3})+$)
replace(txt,pat4,",")
ans = "123,456,789"
  1 Commento
Stephen23
Stephen23 il 14 Nov 2023
vec = 1234.56789;
txt = string(vec);
pat1 = lookBehindBoundary(digitsPattern); % (?<=\d)
pat2 = asManyOfPattern(digitsPattern(3),1); % (\d{3})+
pat3 = lookAheadBoundary(pat2+lineBoundary("end")); % (?=(\d{3})+$)
pat4 = pat1+pat3; % (?<=\d)(?=(\d{3})+$)
replace(txt,pat4,",")
ans = "1234.5,679"

Accedi per commentare.

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by