Inserting a 1000 separator
81 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2013-2-8
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
2013-2-8
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
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!
Image Analyst
2020-9-15
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);
更多回答(2 个)
Jan
2013-2-8
Please take the time to search in the forum at first before posting a new question:
0 个评论
Toshiaki Takeuchi
2023-11-14
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 个评论
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,",")
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!