Inserting a 1000 separator
Mostrar comentarios más antiguos
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 comentarios
Miroslav Balda
el 8 de Feb. de 2013
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
el 8 de Feb. de 2013
Image Analyst
el 8 de Feb. de 2013
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?
Respuesta aceptada
Más respuestas (3)
Jan
el 8 de Feb. de 2013
0 votos
Please take the time to search in the forum at first before posting a new question:
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,",")
1 comentario
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,",")
Joe
hace alrededor de 2 horas
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 comentario
Walter Roberson
hace alrededor de 1 hora
"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.
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!