comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: printing comma delimiters for float variables
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: printing comma delimiters for float variables [message #39586] Thu, 27 May 2004 08:49 Go to previous message
C.E. Ordonez is currently offline  C.E. Ordonez
Messages: 10
Registered: May 2004
Junior Member
"Benjamin Hornberger" <benjamin.hornberger@stonybrook.edu> wrote in message
news:40b4d5c2$1_4@marge.ic.sunysb.edu...
> Hi all,
>
> I am still desperately trying to understand format codes ;-). Is there a
> way to print floating point variables with comma delimiters every 1000,
> e.g. 1,453,234.80?
>
> Thanks,
> Benjamin

Here's a quick-and-dirty function that does what you want. Unfortunately, it
doesn't use FORMAT.

-Caesar

; putcomma: convert real number to comma-separated string
; C.E. Ordonez 27-May-04

function putcomma, number
result = '0.00'
; Check for input
if n_params( ) lt 1 then begin
message, 'missing input argument', /INFO & return, result
endif
; Allowed data types
allowed = [ 2, 3, 4, 5, 12, 13, 14, 15 ]
b = where( allowed eq size( number, /TYPE ), count )
if count ne 1 then begin
message, 'invalid input type', /INFO & return, result
endif
; Start with real number
x = double( number )
; Drop sign for now
y = abs( x )
z = long( y )
; Work on the decimal portion first
r = fix( round( 100.0 * ( y - z ) ) )
result = strcompress( string( r ), /REMOVE_ALL )
if r lt 10 then result = '0' + result
result = '.' + result
; Now work on the integer portion
t = strcompress( string( z ), /REMOVE_ALL )
n = strlen( t )
if n le 3 then begin
; If number is less than 1000, no comma is needed
result = strcompress( string( t ), /REMOVE_ALL ) + result
endif else begin
; Otherwise, recursively shift three digits to left
while n gt 3 do begin
p = strmid( t, n-3, n )
result = ',' + p + result
t = strmid( t, 0, n-3 )
n = strlen( t )
endwhile
result = t + result
endelse
; Put back sign
if x lt 0 then result = '-' + result
return, result
end
[Message index]
 
Read Message
Read Message
Previous Topic: extracting contours from IDL meshs
Next Topic: Uncaught PARTICLE_TRACE exception

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:22:41 PDT 2025

Total time taken to generate the page: 0.00424 seconds