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 日間)
古いコメントを表示
MathWorks Support Team
2010 年 1 月 18 日
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
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
0 件のコメント
その他の回答 (1 件)
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,",")
1 件のコメント
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,",")
参考
カテゴリ
Help Center および File Exchange で Introduction to Installation and Licensing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!