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

18 次查看(过去 30 天)

采纳的回答

MathWorks Support Team
编辑: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 CenterFile 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!

Translated by