How do I add commas to numbers using fprintf?

Hello,
How do I add commas to numbers using fprintf? My customer wants me to write out tables and have commas in the numbers to better read it. For instance, 123456789.0 would be written out as 123,456,789.0. Thanks.
Best regards,
Bill Rooker

 채택된 답변

Stephen23
Stephen23 2017년 3월 8일
편집: Stephen23 2023년 9월 15일
fprintf cannot do that, but you will find several FEX submissions that can create a string with commas as thousands separators:
If you read the comments to those submissions you will also find several improvements to consider.
Another easy one-line approach:
str = ["1234567890.123456789";"-1234567890";"0"]
str = 3×1 string array
"1234567890.123456789" "-1234567890" "0"
str = regexprep(str,'(?<!\.\d*)\d{1,3}(?=(\d{3})+\>)','$&,')
str = 3×1 string array
"1,234,567,890.123456789" "-1,234,567,890" "0"

댓글 수: 4

Bill Rooker
Bill Rooker 2017년 3월 9일
이동: Dyuman Joshi 2023년 9월 15일
Thank you!
@Stephen23, could you recommend some good resources for learning the use of regexp() and regexprep() in MATLAB?
Stephen23
Stephen23 2023년 9월 15일
편집: Stephen23 2023년 9월 15일
The only resources I really use are the MATLAB documentation:
etc.
You can find some online tutorials etc that explain regular expression concepts** but these often are based on Perl or JavaScript or Python or whatever regular expressions (i.e. not the same details) so once you really need to know the details then the only place to look is the MATLAB documentation. Be prepared to read those pages many times.
If you want to experiment then you might find this useful:
** as well as many websites offering all kinds of automagic things with regular expressions.
I tried working with the documentation pages of regexp() and regexprep(), but haven't been successful with it, as they are not clear/specific enough to me and do not provide sufficient examples to understand the functionality properly.
Thank you very much for the links, I didn't know about these pages, they are what I was looking for!
I have also tried a few online tutorials, but as you mentioned, the functionality of regexp() in other languages are different than in MATLAB.
I will also look into the FEX submission you have link, it does look very extensive, but for now, I will start from the basics with the documentation pages you linked.
Once again, Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Ted Shultz
Ted Shultz 2018년 6월 13일

9 개 추천

you can do this in two lines. from this page : A simple way is to add this two line function:
function numOut = addComma(numIn)
jf=java.text.DecimalFormat; % comma for thousands, three decimal places
numOut= char(jf.format(numIn)); % omit "char" if you want a string out
end
Hope that helps! --ted

댓글 수: 5

Roscoe Cook
Roscoe Cook 2019년 5월 15일
Worked great for me! Perhaps this should be the accepted answer. Thanks!
Stargaet
Stargaet 2019년 7월 19일
very good! thanks!
Thanks! Extended it to 2d matrix.
function B = AddCommaArr(A)
B = strings(size(A));
for i = 1:size(A,1)
for j = 1:size(A,2)
B(i,j) = addComma(A(i,j));
end
end
function numOut = addComma(numIn)
jf=java.text.DecimalFormat; % comma for thousands, three decimal places
numOut= char(jf.format(numIn)); % omit "char" if you want a string out
end
end
Daven Gooden
Daven Gooden 2020년 11월 10일
@Ted Shultz: Spot on! Very clean and easy to understand/use! Thank you very mutch!
Edwin Cortez
Edwin Cortez 2022년 2월 2일
편집: Edwin Cortez 2022년 2월 2일
Great ! Thanks

댓글을 달려면 로그인하십시오.

Jim Svensson
Jim Svensson 2021년 2월 15일

9 개 추천

I feel that in 2021 this functionality should be supported by sprintf/fprintf.

댓글 수: 3

Jim Svensson
Jim Svensson 2023년 9월 15일
이동: Dyuman Joshi 2023년 9월 15일
I feel that in 2023 this functionality should be supported by sprintf/fprintf.
@Jim Svensson If you want to have a functionality implemented in MATLAB, you can raise a feature request for it.
I believe you will need a strong justification to convince TMW as to why they should implement it.
It would certainly be a convenience if they had a letter to do that conversion, like p or whatever (some unused letter), like
x = 12345678.912345
fprintf('Number = %p.\n', x);
'12,345,678.912'

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2023년 9월 15일
See my attached utility function that inserts commas to make a string out of a number. Then in your fprintf you print the string with %s instead of %d or %f. For example:
number = 1234567;
fprintf('The number is %s.\n', CommaFormat(number));
The number is 1,234,567.
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

vec = 1234.56789
vec = 1.2346e+03
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"

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2017년 3월 8일

댓글:

2023년 11월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by