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
採用された回答
その他の回答 (4 件)
Ted Shultz
2018 年 6 月 13 日
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
2019 年 5 月 15 日
Worked great for me! Perhaps this should be the accepted answer. Thanks!
Stargaet
2019 年 7 月 19 日
very good! thanks!
Kouichi C. Nakamura
2020 年 2 月 15 日
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
2020 年 11 月 10 日
@Ted Shultz: Spot on! Very clean and easy to understand/use! Thank you very mutch!
Edwin Cortez
2022 年 2 月 2 日
編集済み: Edwin Cortez
2022 年 2 月 2 日
Great ! Thanks
Jim Svensson
2021 年 2 月 15 日
9 投票
I feel that in 2021 this functionality should be supported by sprintf/fprintf.
3 件のコメント
Jim Svensson
2023 年 9 月 15 日
移動済み: Dyuman Joshi
2023 年 9 月 15 日
I feel that in 2023 this functionality should be supported by sprintf/fprintf.
Dyuman Joshi
2023 年 9 月 15 日
@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.
Image Analyst
2023 年 9 月 16 日
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'
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));
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 件のコメント
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,",")
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!