Filter löschen
Filter löschen

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

28 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 28 Mai 2021
Bearbeitet: MathWorks Support Team am 28 Mai 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

Weitere Antworten (1)

Toshiaki Takeuchi
Toshiaki Takeuchi am 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 Kommentar
Stephen23
Stephen23 am 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"

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by