how add "$" and "' ' " in array string
Show older comments
>> 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 Comments
Voss
on 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"?
Answers (3)
Dyuman Joshi
on 16 Aug 2023
Edited: Dyuman Joshi
on 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 Comments
piero
on 16 Aug 2023
piero
on 16 Aug 2023
piero
on 16 Aug 2023
load('matlab_prof.mat')
%Split into integer and decimal parts
%and convert to strings
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
load('matlab_prof.mat')
format long g
disp(prof(18));
%Split into integer and decimal parts
%and convert to strings
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;
disp(out(18));
Better to use mod(abs()) or abs(rem()) in this case:
disp(prof(18));
%Split into integer and decimal parts
%and convert to strings
T1 = string(fix(prof));
T2 = erase(compose("%g",abs(rem(prof,1))),"0.");
%Add separator for thousand place
T1 = regexprep(T1,'(\d+)(\d{3})$',"$1'$2");
%join the strings
out = "$ "+ T1 + "." + T2;
disp(out(18));
Dyuman Joshi
on 16 Aug 2023
I did look into the issue as you pointed out, but it seems that I was not thorough enough in haste.
piero
on 16 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
@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
format long G
V = [0;-23;123.456;-0.78;9;1234567.89;-987654321;7;-54321]
S = compose("$ %.2f",V(:));
S = regexprep(S,"(\d{1,3})(?=(\d{3})+(\D|$))","$1'")
% 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 Comment
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!