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 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: printing comma delimiters for float variables [message #39586] Thu, 27 May 2004 08:49
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
Re: printing comma delimiters for float variables [message #39595 is a reply to message #39586] Wed, 26 May 2004 18:04 Go to previous message
Kenneth P. Bowman is currently offline  Kenneth P. Bowman
Messages: 585
Registered: May 2000
Senior Member
In article <40b4d5c2$1_4@marge.ic.sunysb.edu>,
Benjamin Hornberger <benjamin.hornberger@stonybrook.edu> wrote:

> 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?

I don't know how to do this with standard formats.

You can do it yourself, but it will be a little ugly.

IDL> x = 1453234.80D0
IDL> millions = LONG(x)/10^6
IDL> thousands = (LONG(x)/10^3) MOD 10^3
IDL> ones = LONG(x) MOD 10^3
IDL> fraction = LONG(100*(x - LONG(x)))
IDL> PRINT, millions, thousands, ones, fraction, $
IDL> FORMAT = "(I3,',',I3,',',I3,'.',I2)"
1,453,234.80

The trick will be handling numbers with different magnitudes.
(You need double precision above to get sufficient precision
to get the fraction right.)

Good luck, Ken Bowman
  Switch to threaded view of this topic Create a new topic Submit Reply
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 13:36:16 PDT 2025

Total time taken to generate the page: 0.00552 seconds