how add "$" and "' ' " in array string
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
>> 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 Kommentare
Voss
am 16 Aug. 2023
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"?
Antworten (3)
Dyuman Joshi
am 16 Aug. 2023
Bearbeitet: Dyuman Joshi
am 16 Aug. 2023
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 Kommentare
Stephen23
am 17 Aug. 2023
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
am 17 Aug. 2023
@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
am 16 Aug. 2023
% 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;
1 Kommentar
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!