; $Id: dm_to_string.pro,v 1.8 2005/02/07 20:03:59 qiuym Exp $
;#######################################################################
;
; NAME:
;  dm_to_string
;
; PURPOSE:
;  convert numbers to string
;  if number is !values.f_nan then return ''
;
; CATEGORY:
;  general
;
; AUTHOR:
;  Yiming Qiu
;  NIST Center for Neutron Research
;  100 Bureau Drive, Gaithersburg, MD 20899-8562
;  United States
;  yiming.qiu@nist.gov
;  Feb, 2005
;
; LICENSE:
;  The software in this file is written by an employee of
;  National Institute of Standards and Technology
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used or if the code in this file is
;  included in another product.
;
;#######################################################################

function dm_to_string,number,int=int
    tmp=size(number,/type)
    if tmp eq 7 then return,strtrim(number,2)  ;a string
    n_num = n_elements(number)
    n_end = 0   ;for smart '.0' ending
    if n_num eq 0 then return, ''
    str = strarr(n_num)
    for i=0,n_num-1 do begin
       if finite(number[i]) then begin
         if keyword_set(int) then str[i]=strtrim(string(fix(number[i],type=3)),2) $
         else begin
          str[i] = strtrim(string(number[i]),2)
          ;get rid of leading 0's and '+'
          tmp  = strmid(str[i],0,1)
          head = (tmp eq '-')?'-':''
          if tmp eq '+' or tmp eq '-' then $
              str[i] = strmid(str[i],1,strlen(str[i])-1)
          while (strmid(str[i],0,1) eq '0') and (strlen(str[i]) ne 1)  do $
              str[i] = strmid(str[i],1,strlen(str[i])-1)
          if strmid(str[i],0,1) eq '.' then str[i] = '0'+str[i]
          str[i] = head+str[i]
          ;get rid of trailing 0's for floats
          if strpos(str[i],'e') ne -1 then begin
              tmp = strsplit(str[i],'e',/extract,/preserve)
              str[i] = dm_to_string(dm_to_number(tmp[0]))+'e'+dm_to_string(dm_to_number(tmp[1],/int))
          endif else begin
              if strpos(str[i],'.') ne -1 then $
                 str[i] = str[i]+' ' ;add a trailing empty space
              tmp    = strsplit(str[i],'[0]+ ',/reg,/extract)   ;one or more 0 and one blank space
              str[i] = strtrim(tmp[0],2)
              if strmid(str[i],0,1,/reverse_offset) eq '.' then begin
                 str[i] = str[i]+'0'
                 n_end  = n_end+1
              endif
          endelse
         endelse
       endif
    endfor
    if n_end eq n_num then $
        for i=0,n_num-1 do $
            str[i] = strmid(str[i],0,strlen(str[i])-2)  ;get rid of '.0' ending
    return,(n_num eq 1)?str[0]:str
end