how add "$" and "' ' " in array string

>> 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 comentarios

Voss
Voss el 16 de Ag. de 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"?
piero
piero el 16 de Ag. de 2023
Editada: piero el 16 de Ag. de 2023
"$-43385.61" thank
T originally is array double

Iniciar sesión para comentar.

Respuestas (3)

Dyuman Joshi
Dyuman Joshi el 16 de Ag. de 2023
Editada: Dyuman Joshi el 16 de Ag. de 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)
T = 19×1 string array
"$ 139'411.39" "$ 115'944.39" "$ 413'970.912" "$ 124'256.379" "$ 144'673.585" "$ 93'473.162" "$ 334'232.706" "$ 105'488.574" "$ 114'121.302" "$ 126'438.346" "$ -11'956.632" "$ 95'737.662" "$ 169'120.64" "$ -43'385.61" "$ 215'426.368" "$ -137'202.827" "$ 70'333.129" "$ -71'453.588" "$ 47'995.706"

10 comentarios

piero
piero el 16 de Ag. de 2023
thank
piero
piero el 16 de Ag. de 2023
if T is a double array how would it become?
piero
piero el 16 de Ag. de 2023
prof=string(prof);
prof = split(string(prof),".");
Error using split
Element 3 of the text contains 1 matches while the previous elements have 0. All elements must
contain the same number of matches.
if T is a double array how would it become?
piero
piero el 16 de Ag. de 2023
Editada: piero el 16 de Ag. de 2023
this is array double
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
out = 560×1 string array
"$ 0.0" "$ 125.0" "$ 181.25" "$ -95.5" "$ 0.0" "$ 0.0" "$ -873.0" "$ 0.0" "$ 87.5" "$ 12.5" "$ 1'835.75" "$ 0.0" "$ 0.0" "$ -507.5" "$ 0.0" "$ -100.0" "$ 360.0" "$ -1'861.25" "$ 767.5" "$ -1'473.0" "$ 1'239.5" "$ 0.0" "$ -2'037.5" "$ 1'316.25" "$ 1'742.0" "$ -1'240.0" "$ 50.0" "$ -3'437.5" "$ 0.0" "$ 0.0"
Voss
Voss el 16 de Ag. de 2023
Editada: Voss el 16 de Ag. de 2023
@Dyuman Joshi: Be careful with mod() of negative numbers.
load('matlab_prof.mat')
format long g
disp(prof(18));
-1861.75
%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));
$ -1'861.25
Better to use mod(abs()) or abs(rem()) in this case:
disp(prof(18));
-1861.75
%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));
$ -1'861.75
Dyuman Joshi
Dyuman Joshi el 16 de Ag. de 2023
Thanks for pointing out the flaw, @Voss.
I did look into the issue as you pointed out, but it seems that I was not thorough enough in haste.
piero
piero el 16 de Ag. de 2023
thanks
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
out = "$ 9876543'210.0"
@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
out = 6×1 string array
"$ 9'873'210.123" "$ 123'456.0" "$ -415'537.916" "$ -0.2357" "$ 6.66" "$ 0.42069"
function out = ThousandSep(in)
%THOUSANDSEP adds thousands Separators to a 1x1 array.
import java.text.*
v = DecimalFormat;
out = replace(string(v.format(in)),",", "'");
end

Iniciar sesión para comentar.

Stephen23
Stephen23 el 17 de Ag. de 2023
Editada: Stephen23 el 17 de Ag. de 2023
format long G
V = [0;-23;123.456;-0.78;9;1234567.89;-987654321;7;-54321]
V = 9×1
1.0e+00 * 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'")
S = 9×1 string array
"$ 0.00" "$ -23.00" "$ 123.46" "$ -0.78" "$ 9.00" "$ 1'234'567.89" "$ -987'654'321.00" "$ 7.00" "$ -54'321.00"
% 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)
"$ 139,411.39" "$ 115,944.39" "$ 413,970.91" "$ 124,256.38" "$ 144,673.58" "$ 93,473.16" "$ 334,232.71" "$ 105,488.57" "$ 114,121.30" "$ 126,438.35" "-$ 11,956.63" "$ 95,737.66" "$ 169,120.64" "-$ 43,385.61" "$ 215,426.37" "-$ 137,202.83" "$ 70,333.13" "-$ 71,453.59" "$ 47,995.71"
% 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 comentario

piero
piero el 16 de Ag. de 2023
Editada: piero el 16 de Ag. de 2023
I don't think it's the most efficient solution
there is the word reserved "compose" but I don't know how to use it

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 16 de Ag. de 2023

Comentada:

el 17 de Ag. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by