Hi, I am building a table in which I need to insert numbers with a comman 1000 separator and two decimal points. For example: A=11201453.21 % should be A=1,1201,453.21 Any hint about how to do it? Best,

댓글 수: 3

Miroslav Balda
Miroslav Balda 2013년 2월 8일
Does the initial command have a fixed structure? How you say where to put dots? The resulting expression is not MATLAB command. If it has to be, The right side shot be enclosured in braces.
joseph Frank
joseph Frank 2013년 2월 8일
yes the resulting expression is not a matlab command. I want to generate A={'1,1201,453.21'} in matlab
My code gives close to that:
A=11201453.21
stringVersionOfA = CommaFormat(A)
cellVersionOfString = {stringVersionOfA}
In the command window:
A =
11201453.21
stringVersionOfA =
11,201,453.21
cellVersionOfString =
'11,201,453.21'
Can you explain why your second group has 4 numbers (1201) instead of 3? And is that the reason why the code I posted earlier does not meet your requirements?

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2013년 2월 8일

2 개 추천

Here's a function I've used:
%=====================================================================
% Takes a number and inserts commas for the thousands separators.
function [commaFormattedString] = CommaFormat(value)
% Split into integer part and fractional part.
[integerPart, decimalPart]=strtok(num2str(value),'.');
% Reverse the integer-part string.
integerPart=integerPart(end:-1:1);
% Insert commas every third entry.
integerPart=[sscanf(integerPart,'%c',[3,inf])' ...
repmat(',',ceil(length(integerPart)/3),1)]';
integerPart=integerPart(:)';
% Strip off any trailing commas.
integerPart=deblank(integerPart(1:(end-1)));
% Piece the integer part and fractional part back together again.
commaFormattedString = [integerPart(end:-1:1) decimalPart];
return; % CommaFormat

댓글 수: 2

Ursel Thomßen
Ursel Thomßen 2020년 9월 15일
편집: Ursel Thomßen 2020년 9월 15일
This works perfectly! I even exchanged perdiod and comma and can get German numberformat :-)
... As long as I enter numbers for value. Can anybody tell me, what do I need to change if I want to enter variables (1x1) for value?
Thank you in advance!
Not sure what you want to enter. It can already take constants
[commaFormattedString] = CommaFormat(1234567)
or variables
value = 12345678;
[commaFormattedString] = CommaFormat(value)
Do you want to enter a string? If so, you have to convert it to a numerical value first.
value = str2double(str);

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Jan
Jan 2013년 2월 8일

0 개 추천

Please take the time to search in the forum at first before posting a new question:
Toshiaki Takeuchi
Toshiaki Takeuchi 2023년 11월 14일

0 개 추천

Using pattern
vec = 123456789;
txt = string(vec);
pat1 = lookBehindBoundary(digitsPattern); % (?<=\d)
pat2 = asManyOfPattern(digitsPattern(3),1); % (\d{3})+
pat3 = lookAheadBoundary(pat2+lineBoundary("end")); % (?=(\d{3})+$)
pat4 = pat1+pat3; % (?<=\d)(?=(\d{3})+$)
replace(txt,pat4,",")
ans = "123,456,789"

댓글 수: 1

Stephen23
Stephen23 2023년 11월 14일
편집: Stephen23 2023년 11월 14일
Using the OP's example value:
vec = 11201453.21;
txt = string(vec);
pat1 = lookBehindBoundary(digitsPattern); % (?<=\d)
pat2 = asManyOfPattern(digitsPattern(3),1); % (\d{3})+
pat3 = lookAheadBoundary(pat2+lineBoundary("end")); % (?=(\d{3})+$)
pat4 = pat1+pat3; % (?<=\d)(?=(\d{3})+$)
replace(txt,pat4,",")
ans = "11201453.21"

댓글을 달려면 로그인하십시오.

Joe
Joe 2026년 3월 13일 4:16

0 개 추천

For what it's worth, I know this is an older thread, but for anyone who comes across this down the road, I have a simple function which easily solved this guy's issue shortening the decimal which I integrated into Image Analyst's CommaFormat function:
function n=dec(m,d)
format long g % allows for longer decimal point arithmatic, g - cuts out trailing 0's
m=m*10^d; % shift decimal over to the right
n=round(m)/10^d; % round off extra decimal places, and shift decimal back left
endfunction
From this, all I added was a line before the integerPart and decimalParts are separated:
value = dec(value,2); % new line, value has 2 remaining decimal digits
[integerPart, decimalPart]=strtok(num2str(value),'.'); % previously line 5
Hope this helps. It helped me.

댓글 수: 1

Walter Roberson
Walter Roberson 2026년 3월 13일 5:00
"format long g" only affects the display of data using disp() or display() or by having an expression without a semi-colon. It also affects the display of table() variables and struct() . It never affects any computation.

댓글을 달려면 로그인하십시오.

Stephen23
Stephen23 2026년 3월 19일 15:10
편집: Stephen23 2026년 3월 19일 15:21

0 개 추천

Assuming that you have already converted to text:
A = '11201453.21';
F = @(t) regexprep(t,'(?<!(\.|[eE][-+]?)\d*)\d{1,3}(?=(\d{3})+(e|E|\.|\>))', '$&,');
B = F(A)
B = '11,201,453.21'

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2013년 2월 8일

편집:

2026년 3월 19일 15:21

Community Treasure Hunt

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

Start Hunting!

Translated by