how add "$" and "' ' " in array string
조회 수: 4 (최근 30일)
이전 댓글 표시
>> T(:,2)
ans =
19×1 string array
"139411.39"
"115944.39"
"413970.912"
"124256.379"
"144673.585"
"93473.162"
"334232.706"
"105488.574"
"114121.302"
"126438.346"
"-11956.632"
"95737.662"
"169120.64"
"-43385.61"
"215426.368"
"-137202.827"
"70333.129"
"-71453.588"
"47995.706"
i want display this uitable app designer (i want $ and ' for separator 000)
"$ 139'411.39"
"$ 115'944.39"
댓글 수: 2
Voss
2023년 8월 16일
For negative values, do you want the minus sign before or after the dollar sign? That is, should it be "-$43385.61" or "$-43385.61"?
답변 (3개)
Dyuman Joshi
2023년 8월 16일
편집: Dyuman Joshi
2023년 8월 16일
T = ["139411.39"
"115944.39"
"413970.912"
"124256.379"
"144673.585"
"93473.162"
"334232.706"
"105488.574"
"114121.302"
"126438.346"
"-11956.632"
"95737.662"
"169120.64"
"-43385.61"
"215426.368"
"-137202.827"
"70333.129"
"-71453.588"
"47995.706"];
%Split into integer part and decimal part
T = split(T,".");
%Add apostrophe for thousand's place in the integer part
%as mentioned in the problem statement
T(:,1) = regexprep(T(:,1),'(\d+)(\d{3})$',"$1'$2");
%Join the table by columns
%and add the Dollar sign
T = "$ " + join(T,".",2)
댓글 수: 10
Stephen23
2023년 8월 17일
Note that this code includes at most one apostrophe, i.e. it misses the apostrophes for values >=1e6:
prof = 9876543210;
T1 = string(fix(prof));
T2 = erase(compose("%g",mod(prof,1)),"0.");
%Add separator for thousand place
T1 = regexprep(T1,'(\d+)(\d{3})$',"$1'$2");
%join the strings
out = "$ "+ T1 + "." + T2
Dyuman Joshi
2023년 8월 17일
@Stephen23, Yes I am aware of that, as that is what OP has stated in the problem above and I have mentioned that in the comment as well.
In case OP wants to have separator for every thousand's place, this would be a simple approach -
prof = [9873210.123 123456 -randi([1e8 1e9])/1e3 -0.2357 6.66 0.42069]';
T1 = fix(prof);
T2 = erase(string(abs(prof - T1)),"0.");
out = "$ "+arrayfun(@ThousandSep, T1)+"."+T2
function out = ThousandSep(in)
%THOUSANDSEP adds thousands Separators to a 1x1 array.
import java.text.*
v = DecimalFormat;
out = replace(string(v.format(in)),",", "'");
end
Mrutyunjaya Hiremath
2023년 8월 16일
% Sample data
T = [
"139411.39";
"115944.39";
"413970.912";
"124256.379";
"144673.585";
"93473.162";
"334232.706";
"105488.574";
"114121.302";
"126438.346";
"-11956.632";
"95737.662";
"169120.64";
"-43385.61";
"215426.368";
"-137202.827";
"70333.129";
"-71453.588";
"47995.706";
];
% Convert to numerical array
T_num = str2double(T);
% Format the numbers with custom formatting
formattedData = strings(size(T));
for i = 1:length(T_num)
if T_num(i) >= 0
formattedData(i) = sprintf("$ %d,%03d.%02d", floor(T_num(i)/1000), mod(floor(T_num(i)),1000), round(100*mod(T_num(i),1)));
else
T_abs = abs(T_num(i));
formattedData(i) = sprintf("-$ %d,%03d.%02d", floor(T_abs/1000), mod(floor(T_abs),1000), round(100*mod(T_abs,1)));
end
end
disp(formattedData)
% Suppose 'app' is your app struct and 'UITable' is the name of your uitable component
% You can set the Data property as follows:
app.UITable.Data = formattedData;
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!