Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to adjust number format
Date: Tue, 16 Dec 2008 18:09:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 27
Message-ID: <gi8qrv$gjh$1@fred.mathworks.com>
References: <gi8he3$dvs$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1229450943 17009 172.30.248.37 (16 Dec 2008 18:09:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 16 Dec 2008 18:09:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1343420
Xref: news.mathworks.com comp.soft-sys.matlab:507351


"sujata" <sujatagp@gmail.com> wrote in message <gi8he3$dvs$1@fred.mathworks.com>...
> Hi,
> 
> Does anyone know if you have a large number like for instance 1500000 how it can be adjusted to a nicer format such as 1,500,000. I know its not possible that a number is given in this way in Matlab, but can it be somehow converted into a string of such a format? 
> 
> Thanks
> Sujata 
---------------------------------------------------------
I don't know if anything is built-in to MATLAB.  Perhaps this function I wrote will help you:

%=====================================================================
% Takes a function 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