Clear Filters
Clear Filters

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

3 views (last 30 days)
piero
piero on 16 Aug 2023
Commented: Dyuman Joshi on 17 Aug 2023
>> 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
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"?
piero
piero on 16 Aug 2023
Edited: piero on 16 Aug 2023
"$-43385.61" thank
T originally is array double

Sign in to comment.

Answers (3)

Dyuman Joshi
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)
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 Comments
Stephen23
Stephen23 on 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
out = "$ 9876543'210.0"
Dyuman Joshi
Dyuman Joshi on 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
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

Sign in to comment.


Stephen23
Stephen23 on 17 Aug 2023
Edited: Stephen23 on 17 Aug 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"

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath on 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)
"$ 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 Comment
piero
piero on 16 Aug 2023
Edited: piero on 16 Aug 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

Sign in to 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!