How do I insert the comma marker for thousands into large numbers while printing to file or Command Line in MATLAB 7.7 (R2008b)?
Show older comments
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
Accepted Answer
More Answers (2)
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,",")
1 Comment
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,",")
Assuming that you have already converted to text:
A = '1200387';
F = @(t) regexprep(t,'(?<!(\.|[eE][-+]?)\d*)\d{1,3}(?=(\d{3})+(e|E|\.|\>))', '$&,');
B = F(A)
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!