MATLAB Answers

Inserting a 1000 separator

128 views (last 30 days)
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,
Image Analyst
Image Analyst on 8 Feb 2013
My code gives close to that:
stringVersionOfA = CommaFormat(A)
cellVersionOfString = {stringVersionOfA}
In the command window:
A =
stringVersionOfA =
cellVersionOfString =
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?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 8 Feb 2013
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.
% Insert commas every third entry.
integerPart=[sscanf(integerPart,'%c',[3,inf])' ...
% Strip off any trailing commas.
% Piece the integer part and fractional part back together again.
commaFormattedString = [integerPart(end:-1:1) decimalPart];
return; % CommaFormat
Image Analyst
Image Analyst on 15 Sep 2020
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);

Sign in to comment.

More Answers (1)

Jan on 8 Feb 2013
Please take the time to search in the forum at first before posting a new question:

Community Treasure Hunt

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

Start Hunting!

Translated by