function formatFcn = getNumberFormatFcn(imgmodel)
%getNumberFormatFcn Function handle that returns a formatted number string.
%   formatFcn = getNumberFormatFcn(IMGMODEL) returns a function
%   handle. formatFcn has this signature:
%
%       STR = formatFcn(V)
%
%   where STR is a formatted string representation of V, a scalar.
%
%   IMGMODEL is expected to contain one image model object.
%
%   Example
%   -------  
%       This example shows how the formatted string depends on the
%       image class type.
%
%       I = imread('snowflakes.png');
%       h = imshow(I)
%       im = imagemodel(h);
%       formatFcn = getNumberFormatFcn(imgmodel)
%       string = formatFcn(I(1,1))
%
%       I = im2single(I);
%       h = imshow(I);  
%       im = imagemodel(h);
%       formatFcn = getNumberFormatFcn(im)
%       string = formatFcn(I(1,1))
%
%   See also IMAGEMODEL.

%   Copyright 1993-2004 The MathWorks, Inc.
%   $Revision: 1.1.8.1 $  $Date: 2004/08/10 01:49:48 $

imgmodel = checkForMultipleImageModels(imgmodel,mfilename);
imageClass = getClassType(imgmodel);

switch imageClass  
  case {'int16','uint16','uint8','logical'}
   formatFcn = @(value) sprintf('%d',value);
  
  case {'double','single'}
   img = get(imgmodel.ImageHandle,'CData');
   
   if isequal(floor(img),img) && all(isfinite(img(:)))
     formatFcn = @(value) sprintf('%d',value);
   else
     absMaxVal = abs(max(img(:)));
     if (absMaxVal >= 10^4 || absMaxVal < 10^-2)
       formatFcn = @(value) sprintf('%1.2E', value);
     else
       formatFcn = @(value) sprintf('%1.2f',value);        
     end
   end
  
  otherwise
   eid = sprintf('Images:%s:internalError',mfilename);
   msg = 'Internal error: invalid class type.';
   error(eid,'%s',msg);
end
