Thread Subject: how to adjust number format

Subject: how to adjust number format

From: sujata

Date: 16 Dec, 2008 15:28:03

Message: 1 of 4

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

Subject: how to adjust number format

From: Image Analyst

Date: 16 Dec, 2008 18:09:03

Message: 2 of 4

"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

Subject: how to adjust number format

From: us

Date: 16 Dec, 2008 19:14:02

Message: 3 of 4

"sujata"
> 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...

one of the many (here vectorized) solutions

% the data
     mrk=','; % <- select your marker
     a=[-50125,1,1500000,1250125,int64(inf)];
% the engine
     clear r;
     v=sprintf('%30.0f',a); % - works up to INTMAX('uint64')
     v=reshape(v,[],numel(a));
     vx=repmat([1,1,1,0]~=0,1,10);
     vx(end)=[];
     r(vx,:)=v;
     r(~vx,:)=mrk;
     r=cellstr(r.');
     r=strrep(r,[' ',mrk],'');
     r=strtrim(r);
% the result
     disp(r);
%{
     '-50,125'
     '1'
     '1,500,000'
     '1,250,125'
     '9,223,372,036,854,775,800'
%}

us

Subject: how to adjust number format

From: sujata

Date: 17 Dec, 2008 10:27:01

Message: 4 of 4

It works!!

Thanks a million!

"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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
cellstr us 16 Dec, 2008 14:15:16
strtrim us 16 Dec, 2008 14:15:16
code us 16 Dec, 2008 14:15:16
logical indexing us 16 Dec, 2008 14:15:16
strrep us 16 Dec, 2008 14:15:16
sprintf us 16 Dec, 2008 14:15:16
number change sujata 16 Dec, 2008 10:30:35
format sujata 16 Dec, 2008 10:30:35
rssFeed for this Thread

Contact us at files@mathworks.com