フィルターのクリア

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 ビュー (過去 30 日間)
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

採用された回答

MathWorks Support Team
MathWorks Support Team 2021 年 5 月 28 日
編集済み: MathWorks Support Team 2021 年 5 月 28 日
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

その他の回答 (1 件)

Toshiaki Takeuchi
Toshiaki Takeuchi 2023 年 11 月 14 日
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 件のコメント
Stephen23
Stephen23 2023 年 11 月 14 日
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"

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by